[erlang-questions] gen_tcp send non-blocking in erlang?

Fred Hebert <>
Mon Mar 17 21:28:48 CET 2014


To note that erlang:port_command(TcpSocket, DataToSend, []) will
deschedule the process (and leave it that way) when the buffer is full.
The alternative is to pass in the 'nosuspend' option, which will avoid
the issue, but will return true/false depending on if it was possible to
send the packet to the buffer.

On 03/17, Geoff Cant wrote:
> gen_tcp is a wrapper around functionality provided by a prim_inet port - and in fact gen_tcp implements a synchronous call interface to the underlying asynchronous message based port API, so if you are prepare to thoroughly void your warranty, you can do async tcp sends in Erlang.
> 
> A testament to my sins^W^W^W^Wworked example can be seen here: https://github.com/heroku/logplex/blob/master/src/logplex_tcpsyslog_drain.erl#L565, https://github.com/heroku/logplex/blob/master/src/logplex_tcpsyslog_drain.erl#L224
> 
> * erlang:port_command(TcpSocket, DataToSend, []) % Start async send
> * receive {inet_reply, TcpSocket, Result} -> something() end % finish async send - result is 'ok' or {'error', Why}
> 
> You buy yourself a whole lot of hurt with this - you have to manage all your own timeouts and error detection behaviour. The upside is that the process doing the sending doesn't need to block on a selective receive, so can continue doing work (clearing a noisy incoming message queue in my example above).
> 
> 
> tl;dr: yes, it can be done but think twice before doing so, and don't bug the OTP team if it goes wrong - they tried to warn us :)
> 
> -Geoff
>  
> 
> On 2014-03-17, at 00:15 , Vinoth Kumar <> wrote:
> 
> > Does gen_tcp supports non-blocking send ?
> > 
> > I have tried gen_tcp:send which returns the ok while the packets has not been sent to the receiver. Whether any mechanism in erlang to check the sent message delivary?
> > 
> > 
> > 
> > Thanks,
> > 
> > Vinoth Kumar J
> > 
> 
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list