title: Are nonblocking I/O operations in Perl limited to one thread? Good design?
body: <p>I am attempting to develop a service that contains numerous client and server sockets (a server service as well as clients that connect out to managed components and persist) that are synchronously polled through <code>IO::Select</code>. The idea was to handle the I/O and/or request processing needs that arise through pools of worker threads.</p>
  
  <p>The <code>shared</code> keyword that makes data shareable across threads in Perl (<code>threads::shared</code>) has its limits--handle references are not among the primitives that can be made shared.</p>
  
  <p>Before I figured out that handles and/or handle references cannot be shared, the plan was to have a <code>select()</code> thread that takes care of the polling, and then puts the relevant handles in certain <code>ThreadQueue</code>s spread across a thread pool to actually do the reading and writing.  (I was, of course, designing this so that modification to the actual descriptor sets used by <code>select</code> would be thread-safe and take place in one thread only--the same one that runs <code>select()</code>, and therefore never while it's running, obviously.) </p>
  
  <p>That doesn't seem like it's going to happen now because the handles themselves can't be shared, so the polling as well as the reading and writing is all going to need to happen from one thread.  Is there any workaround for this?  I am referring to the decomposition of the actual system calls across threads;  clearly, there are ways to use queues and buffers to have data produced in other threads and actually sent in others.</p>
  
  <p>One problem that arises from this situation is that I have to give <code>select()</code> a timeout, and expect that it'll be high enough to not cause any issues with polling a rather large set of descriptors while low enough not to introduce too much latency into my timing event loop - although, I do understand that if there is actual I/O set membership detected in the polling process, <code>select()</code> will return early, which partly mitigates the problem.  I'd rather have some way of waking <code>select()</code> up from another thread, but since handles can't be shared, I cannot easily think of a way of doing that nor see the value in doing so;  what is the other thread going to know about when it's appropriate to wake <code>select()</code> anyway?</p>
  
  <p>If no workaround, what is a good design pattern for this type of service in Perl?  I have a requirement for a rather high amount of scalability and concurrent I/O, and for that reason went the nonblocking route rather than just spawning threads for each listening socket and/or client and/or server process, as many folks using higher-level languages these days are wont to do when dealing with sockets - it seems to be kind of a standard practice in Java land, and nobody seems to care about <code>java.nio.*</code> outside the narrow realm of systems-oriented programming.  Maybe that's just my impression.  Anyway, I don't want to do it that way.</p>
  
  <p>So, from the point of view of an experienced Perl systems programmer, how should this stuff be organised?  Monolithic I/O thread + pure worker (non-I/O) threads + lots of queues?  Some sort of clever hack?  Any thread safety gotchas to look out for beyond what I have already enumerated?  Is there a Better Way?  I have extensive experience architecting this sort of program in C, but not with Perl idioms or runtime characteristics.</p>
  
  <p>EDIT: P.S.  It has definitely occurred to me that perhaps a program with these performance requirements and this design should simply not be written in Perl.  But I see an awful lot of very sophisticated services produced in Perl, so I am not sure about that.</p>