Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## There is a problem with this code, which causes the whole process to crash.
- defp loop(module, state) do
- new_state = receive do
- {request, sender} ->
- {response, new_state} = module.handle_call(request, state)
- send(sender, {:response, response})
- end
- IO.inspect(new_state)
- loop(module, new_state)
- end
- # c("server_process2.ex")
- # pid = ServerProcess.start(KeyValueStore)
- # KeyValueStore.put(pid, :name, :yc)
- new_state is returned as {:response, :ok}
- module.handle_call( {:get, :name}, {:response, :ok})
- ## To fix this problem,
- ## You have to move the loop inside the block
- def loop(module, state)
- receive do
- # you can pattern match against the request, to make the request a little clearer
- {request, sender} ->
- {response, new_state} = module.handle_call(request, state)
- loop(module, new_state)
- end
- # 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