dash2 has asked for the wisdom of the Perl Monks concerning the following question:
So I looked into IO::Select and non-blocking IO, and wrote (something like) this:
while (1) { print "yo\n"; while ( my @handles = $self->{daemon}->can_read(1) ) { foreach ( @handles ) { if ($_ == $self->{listener}) { my $new_sock = $self->{listener}->accept or die "accept: $!"; $self->{daemon}->add( $new_sock); } else { my @rawmsg = $_->getlines; print @rawmsg; #$_->close; } } } }
Where,as you may guess $self->{daemon} is an IO::Select and $self->{listener} is a listening IO::Socket object. So I am trying to listen for calls, while periodically printing "yo". One day, "yo" will be replaced by interesting stuff.
My problem is this: if I don't do close on new sockets, the socket blocks, and I get:
yo yo yo yo message from client
and then nothing. But if I include the close() call, then the socket stops blocking, but can_read doesn't wait at all next time. I get:
yo yo yo yo message from client yo yo yo
with an infinite row of yos, and no more client messages.
How do I get can_read to be good second time round? More generally, is this a good way to go about things? I thought about forking a message receiver which would then pass messages via a pipe... but then I thought that I'd need to block while waiting for the pipe... seems like I need a language with better multithreading but maybe someone can tell me better...
best
dave hj~
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: non-blocking select with IO::Select
by c-era (Curate) on Jul 03, 2001 at 18:19 UTC | |
by natebailey (Acolyte) on Mar 23, 2003 at 08:35 UTC | |
|
(tye)Re: non-blocking select with IO::Select
by tye (Sage) on Jul 03, 2001 at 18:42 UTC | |
|
Re: non-blocking select with IO::Select
by pope (Friar) on Jul 03, 2001 at 18:23 UTC | |
|
Re: non-blocking select with IO::Select
by dash2 (Hermit) on Jul 03, 2001 at 20:13 UTC | |
by c-era (Curate) on Jul 11, 2001 at 19:37 UTC |