Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. ## There is a problem with this code, which causes the whole process to crash.
  2.  
  3. defp loop(module, state) do
  4. new_state = receive do
  5. {request, sender} ->
  6. {response, new_state} = module.handle_call(request, state)
  7. send(sender, {:response, response})
  8. end
  9. IO.inspect(new_state)
  10. loop(module, new_state)
  11. end
  12.  
  13. # c("server_process2.ex")
  14. # pid = ServerProcess.start(KeyValueStore)
  15. # KeyValueStore.put(pid, :name, :yc)
  16.  
  17. new_state is returned as {:response, :ok}
  18. module.handle_call( {:get, :name}, {:response, :ok})
  19.  
  20. ## To fix this problem,
  21. ## You have to move the loop inside the block
  22. def loop(module, state)
  23. receive do
  24. # you can pattern match against the request, to make the request a little clearer
  25. {request, sender} ->
  26. {response, new_state} = module.handle_call(request, state)
  27. loop(module, new_state)
  28. end
  29.  
  30.  
  31. # I wonder what happens when you do not have a receive block to handle incoming messages.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement