[erlang-questions] gen_tcp send blocks gen_tcp recv.

Tim Watson <>
Tue Sep 4 23:09:40 CEST 2012


On 4 Sep 2012, at 08:45, Satyam Shekhar wrote:

> @erik: I wasn't sure if this was the standard practice. Also, this complicates the architecture. Now, I ll have 2 processes, that manage a tcp socket. One for receiving and one for sending, both having reference to the same socket. Is this ok?

That is not at all unusual in Erlang.

> 
> @arun: The article on trapexit only show how to do async accept. It doesnt do async send of gen_tcp socket. 
> To elaborate on the problem - I have one process that is an abstraction over the gen_tcp socket. It does xml parsing on the data received on the tcp socket and forwards parsed data to the controlling process. Also it receives the xml from a process and sends it on the tcp socket. Lets call this smart socket. Now, if the process that sends data to  smart socket, sends too much of it, smart socket starts to starts blocking on send calls.
> 
> How do people usually handle such scenarios?
> 

Well that just depends on how the client(s) will use the socket. In some protocols it makes good sense to block during send, and in others not so much. The canonical way to do this is with multiple processes sharing the socket, but of course you can just port_command to send data (asynchronously) - the writer process in RabbitMQ does that as well, see rabbit_writer which is called by rabbit_channel. You will notice that some writes are blocking and others are not (using port_command to write).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 235 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120904/62305933/attachment.bin>


More information about the erlang-questions mailing list