dbar1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to use select call in multi-client model. Here's what my code does.

# Create a socket $sockSel = new IO::Select( $servSock ); while ( @ready = $sockSel->can_read ) { foreach $fh ( @ready ) if ( $fh == $servSock ) { $newConn = $servSock->accept(); $sockSel->add($newConn); } else { $fh->recv($sockBuff,$maxLenMsg); if($sockBuff) { # Process sockBuff } } }

Now, when one client connects, my code blocks till the sockBuff from that client is processed. In the meantime, it cannot serve any other clients. Is there a way around this? I want to serve the next client when the 1st client is getting processed. Can I avoid threads and still achieve this with select? Thanks

Edit: g0n - code tags and formatting

Replies are listed 'Best First'.
Re: Using IO::Select in multi-client environment
by marto (Cardinal) on Feb 16, 2007 at 17:56 UTC
Re: Using IO::Select in multi-client environment
by ikegami (Patriarch) on Feb 16, 2007 at 20:02 UTC

    If your "Process sockBuff" code blocks or takes a long time, then yes, you won't be able to process other incoming connections or incoming data.

    One solution is to spawn a thread or process to do the work.

    Another solution would be to do co-operative multitasking. "Process sockBuff" would be divided into smaller tasks, and can_read would be checked in between those smaller tasks.

Re: Using IO::Select in multi-client environment
by Joost (Canon) on Feb 17, 2007 at 12:21 UTC