<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1528" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=978301807-28032006>Difficult - I'd use method 2. I guess it
really depends upon what you want to do later.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=978301807-28032006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=978301807-28032006>You
code looks very much like <A
href="http://www.sics.se/~joe/tutorials/web_server/tcp_server.erl">http://www.sics.se/~joe/tutorials/web_server/tcp_server.erl</A></SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=978301807-28032006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN class=978301807-28032006>You
might like to read <A
href="http://www.sics.se/~joe/tutorials/web_server/web_server.html">http://www.sics.se/~joe/tutorials/web_server/web_server.html</A> which
has some</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=978301807-28032006>discussion about a web sever which spawns a new handler
for every incoming socket.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=978301807-28032006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=978301807-28032006>/Joe</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff size=2><SPAN
class=978301807-28032006></SPAN></FONT> </DIV><FONT face=Arial
color=#0000ff size=2></FONT><BR>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> owner-erlang-questions@erlang.org
[mailto:owner-erlang-questions@erlang.org] <B>On Behalf Of </B>Jérémie
Lumbroso<BR><B>Sent:</B> den 27 mars 2006 20:31<BR><B>To:</B>
erlang-questions@erlang.org<BR><B>Subject:</B> Efficiency: registered process
or loop-function argument?<BR></FONT><BR></DIV>
<DIV></DIV><BR>Hello,<BR><BR>I have server that spawns a new 'handler'
process/function for every incoming socket.<BR><BR>Those 'handler' process
must communicate with the server process. I am wondering which of the two
following solutions is best. <BR><BR>
<DIV style="MARGIN-LEFT: 40px">1. Should the server process register itself
with the server atom, as in:</DIV>
<DIV style="MARGIN-LEFT: 80px"><SPAN
style="FONT-FAMILY: courier new,monospace"><BR><SPAN
style="FONT-WEIGHT: bold">start</SPAN>() -><BR>
[...]<BR> register(server, spawn(test, </SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-WEIGHT: bold">server_loop</SPAN></SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"> , [LSock])).<BR><BR><SPAN
style="FONT-WEIGHT: bold">server_loop</SPAN>(LSock) -></SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace"> case
gen_tcp:accept(LSock, 1000) of </SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
{ok, Socket} -></SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"></SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
[...]</SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"><BR>
Client_Pid = </SPAN><SPAN
style="FONT-FAMILY: courier new,monospace">spawn(?MODULE, <SPAN
style="FONT-WEIGHT: bold">relay</SPAN>, [Socket]),</SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
gen_tcp:controlling_process(Socket,
Client_Pid), </SPAN><BR style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
</SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-WEIGHT: bold">server_loop</SPAN> </SPAN><SPAN
style="FONT-FAMILY: courier new,monospace">(LSock);<BR><BR
style="FONT-FAMILY: courier new,monospace"></SPAN><SPAN
style="FONT-FAMILY: courier new,monospace">
[...]</SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"> </SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
end.<BR><BR><SPAN style="FONT-WEIGHT: bold">relay</SPAN>(Socket)
-><BR> receive<BR>
{tcp, Socket, Bin} -> <BR>
Data = binary_to_list(Bin),<BR>
server ! {self(),
Data},<BR>
?MODULE:<SPAN
style="FONT-WEIGHT: bold">relay</SPAN>(Socket);<BR><BR>
[...]<BR> end.<BR><BR></SPAN></DIV>
<DIV style="MARGIN-LEFT: 40px"><BR>2. Should I pass the server's pid as an
argument to the client loop, such as:<BR>
<DIV style="MARGIN-LEFT: 40px"><SPAN
style="FONT-FAMILY: courier new,monospace"><BR><SPAN
style="FONT-WEIGHT: bold">start</SPAN>() -><BR>
[...]<BR> spawn(test, </SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-WEIGHT: bold">server_loop</SPAN></SPAN><SPAN
style="FONT-FAMILY: courier new,monospace">, [LSock]).<BR><BR><SPAN
style="FONT-WEIGHT: bold">server_loop</SPAN>(LSock) -></SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace"> case
gen_tcp:accept(LSock, 1000) of</SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
{ok, Socket} -></SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"></SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
[...]</SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"><BR>
Client_Pid = </SPAN><SPAN
style="FONT-FAMILY: courier new,monospace">spawn(?MODULE, <SPAN
style="FONT-WEIGHT: bold">relay</SPAN>, [Socket, self()]),</SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
gen_tcp:controlling_process(Socket,
Client_Pid),</SPAN><BR style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
</SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-WEIGHT: bold">server_loop</SPAN></SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"> (LSock);<BR><BR
style="FONT-FAMILY: courier new,monospace"></SPAN><SPAN
style="FONT-FAMILY: courier new,monospace">
[...]</SPAN><SPAN
style="FONT-FAMILY: courier new,monospace"></SPAN><BR
style="FONT-FAMILY: courier new,monospace"><SPAN
style="FONT-FAMILY: courier new,monospace">
end.<BR><BR><SPAN style="FONT-WEIGHT: bold">relay</SPAN>(Socket, Server)
-><BR> receive<BR>
{tcp, Socket, Bin} -><BR>
Data = binary_to_list(Bin),<BR>
Server ! {self(),
Data},<BR>
?MODULE:<SPAN style="FONT-WEIGHT: bold">relay</SPAN>(Socket,
Server);<BR><BR>
[...]<BR> end.</SPAN></DIV><BR></DIV><BR>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? <BR><BR>Thanks,<BR><BR>Jérémie
Lumbroso<BR></BLOCKQUOTE></BODY></HTML>