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~
In reply to non-blocking select with IO::Select by dash2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |