IO::Select's can_read method will (by default) block forever until something can be read. If passed an argument, it will only wait as many seconds (possibly fractional) as requested. If you call can_read with an argument of zero, it will return immediately, allowing you to do a non-blocking check to see if you have a readable socket.
For example:
use IO::Select;
$s = IO::Select->new();
$s->add(\*STDIN);
if ($s->can_read(0)) { # Non-blocking check.
# Something to read!
} else {
# Nothing to read.
}
Hope you find this useful.
Cheers,
Paul | [reply] [d/l] |
Thanks!!!...got it working!!!
| [reply] |
If you want to use non-blocking in sockets, you can use select by yourself, or just use IO::Select.
There are three solutions:
- fork
- multiplex the conections with just one process
- use threads
Here's an example of the multiplexion, that i read in advanced perl programing:
use IO::Socket;
use IO::Select;
my $server = IO::Socket::INET->new ...
my $select = IO::Select->new;
my $time = 0.5; # How time it waits to check for a new conection
$select->add($server);
while(1)
{
my ($sockets_ready) = IO::Select->select($select,
undef, undef, $time);
foreach my $sock (@$sockets_ready) {
if ($sock == $server) { # Accept a new conection
my $new_sock = $sock->accept();
$select->add($new_sock);
}
else { # Its an old conection
....
}
}
I dont test the code, hope it works.
$anarion=\$anarion;
s==q^QBY_^=,$_^=$[x7,print
| [reply] [d/l] |
Just a thought: try giving &IO::Select::can_read a timeout value. This should keep it from blocking. I'm not sure what the consequences would be if timeout occurs (I'm working my way slowly through NPwP myself), but it would be fun testing it.
| [reply] |
| [reply] |