[zeromq-dev] equivalent of accept

Brian Candler B.Candler at pobox.com
Wed Apr 21 10:28:36 CEST 2010


On Wed, Apr 21, 2010 at 08:25:51AM +0800, Steven McCoy wrote:
>      Could you also clarify for my benefit: if you have (say) five worker
>      threads
>      which want to handle incoming REQs from clients, should the five
>      threads all
>      call zmq_recv on the same socket, or should the five threads each
>      create
>      their own zmq_socket bound to the same proto/port?
> 
>    From my understanding it is each thread must have it's own ZMQ socket,
>    there is no thread safety for the sake of performance.  Therefore the
>    second option would have to be the only viable route.

Doesn't work:

>> require 'zmq'
=> true
>> ctx = ZMQ::Context.new(2,2,0)
=> #<ZMQ::Context:0xb74dce68>
>> s1 = ctx.socket(ZMQ::REP)
=> #<ZMQ::Socket:0xb74d74f4>
>> s2 = ctx.socket(ZMQ::REP)
=> #<ZMQ::Socket:0xb74d3854>
>> s1.bind("tcp://127.0.0.1:5555")
=> nil
>> s2.bind("tcp://127.0.0.1:5555")
RuntimeError: Address already in use
	from (irb):7:in `bind'
	from (irb):7
	from :0

Since REP/REQ is lock-step, if you want concurrency you need multiple worker
threads.  And since you can't bind multiple sockets to the same IP+port,
they'd have to share the same socket.



More information about the zeromq-dev mailing list