Efficiency: registered process or loop-function argument?

Joe Armstrong (AL/EAB) <>
Tue Mar 28 09:25:01 CEST 2006


Difficult - I'd use method 2.  I guess it really depends upon what you want to do later.
 
You code looks very much like http://www.sics.se/~joe/tutorials/web_server/tcp_server.erl
 
You might like to read http://www.sics.se/~joe/tutorials/web_server/web_server.html which has some
discussion about a web sever which spawns a new handler for every incoming socket.
 
/Joe
 



________________________________

	From:  [mailto:] On Behalf Of Jérémie Lumbroso
	Sent: den 27 mars 2006 20:31
	To: 
	Subject: Efficiency: registered process or loop-function argument?
	
	

	Hello,
	
	I have server that spawns a new 'handler' process/function for every incoming socket.
	
	Those 'handler' process must communicate with the server process. I am wondering which of the two following solutions is best. 
	
	
	1. Should the server process register itself with the server atom, as in:
	
	start() ->
	    [...]
	    register(server, spawn(test, server_loop , [LSock])).
	
	server_loop(LSock) ->
	    case gen_tcp:accept(LSock, 1000) of 
	        {ok, Socket} ->
	            [...]
	            Client_Pid = spawn(?MODULE, relay, [Socket]),
	            gen_tcp:controlling_process(Socket, Client_Pid), 
	            server_loop (LSock);
	
	        [...] 
	    end.
	
	relay(Socket) ->
	    receive
	        {tcp, Socket, Bin} -> 
	            Data = binary_to_list(Bin),
	            server ! {self(), Data},
	            ?MODULE:relay(Socket);
	
	        [...]
	    end.
	
	

	2. Should I pass the server's pid as an argument to the client loop, such as:
	
	
	start() ->
	    [...]
	    spawn(test, server_loop, [LSock]).
	
	server_loop(LSock) ->
	    case gen_tcp:accept(LSock, 1000) of
	        {ok, Socket} ->
	            [...]
	            Client_Pid = spawn(?MODULE, relay, [Socket, self()]),
	            gen_tcp:controlling_process(Socket, Client_Pid),
	            server_loop (LSock);
	
	        [...]
	    end.
	
	relay(Socket, Server) ->
	    receive
	        {tcp, Socket, Bin} ->
	            Data = binary_to_list(Bin),
	            Server ! {self(), Data},
	            ?MODULE:relay(Socket, Server);
	
	        [...]
	    end.


	Or rather, I realize this question is on a per-case basis mostly ... so really, what I would like to ask, is whether there are any side-effects to doing it the second way (by passing an argument). Is the overhead for an extra argument on a loop function like that big? 
	
	Thanks,
	
	Jérémie Lumbroso
	

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060328/6ab88889/attachment.html>


More information about the erlang-questions mailing list