testing asynchronous code

Jay Nelson <>
Wed Apr 21 21:09:09 CEST 2010


Scott wrote:

 > It's quite unusual to have a gen server send itself a message using
 > gen server:cast().  I don't know why your code would do it...

I do find it very useful during initialization.  As you said in a  
different thread, a gen_server should start quickly to avoid causing  
a supervisor timeout when it launches too many slow children.

start_link(...) ->
	{ok, Pid} = gen_server:start_link(...),
	gen_server:cast(Pid, finish_init),
	{ok, Pid}.


The keys to the technique are:

1) Make sure you cast before the Pid leaks out to any callers
2) Make sure enough of the state is initialized for finish_init to run
3) Any callers are safely in the queue after finish_init

jay



More information about the erlang-questions mailing list