Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Does IO::Select work? Anywhere?

by BrowserUk (Pope)
on Oct 21, 2012 at 15:56 UTC ( #1000225=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Does IO::Select work? Anywhere?
in thread Does IO::Select work? Anywhere?

that is why I like using the eventloop method that Glib and Wx use

I guess it explains the popularity of such frameworks, when the primary perl tool -- IO:Select -- has been broken for 12 years or more.

And probably why all the examples are so bare bones. Every time anyone has tried to write anything more complex, it breaks. So they either give up or resort to some higher-level framework.

P.S. See the low level Perl code, multiplexing server at perl examples

Thanks. Unfortunately, the first thing I see in the I/O Multiplexing Server example is:

sub readline { # 'select'-compatible function for reading one line of input from # a filehandle. # # readline() expects one argument -- filehandle S # # sample call: $mystring = readline('S') # my $filehandle = $_[0]; my $c=''; my $retstr=''; my $endoffile=0; while ($c ne "\n" && ! $endoffile) { if (sysread($filehandle, $c, 1) > 0) { $retstr = $retstr . $c; } else { $endoffile=1; } } return $retstr; }

What if the client gets interrupted before it sends the newline?

You've got a something like 900 seconds default before the connection times out and the endoffile flag gets raised; during which time, nothing else happens. No new clients get accepted, no existing clients get serviced.

And this is teaching material??


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


Comment on Re^4: Does IO::Select work? Anywhere?
Download Code
Re^5: Does IO::Select work? Anywhere?
by zentara (Archbishop) on Oct 22, 2012 at 10:35 UTC
    And this is teaching material??

    Sure, it taught me alot. To get away from your obsession on readline, try this slightly modified version. I can kill the client's xterm, before I send any newline, and the server stays responsive to new and other connections.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      To get away from your obsession on readline,

      Observation is not obsession.

      I can kill the client's xterm, before I send any newline, and the server stays responsive to new and other connections.

      Sure you can if you fork, but that's a different solution. Ie. a forking server not a multiplexing server.

      Here is a perfectly serviceable equivalent using threads:

      #! perl -sw use strict; use threads stack_size => 4096; use threads::shared; use IO::Socket; use constant CRLF => chr( 13 ) . chr( 10 ); $/ = $\ = CRLF; my $server = IO::Socket::INET->new( LocalHost => 'localhost', LocalPort => 1025, Listen => SOMAXCONN, Reuse => 1, ) or die "Couldn't create listening socket"; while( 1 ) { my $client = $server->accept; async { my $peerhost = $client->peerhost .':'. $client->peerport; while( <$client> ){ chomp; print $client $_; print "$peerhost:$_"; } }->detach; } close $server;

      That will (has) run all day and night without problems.

      And here is a 'one-liner' that will launch 1000 clients at it each time:

      perl -Mthreads=stack_size,4096 -MIO::Socket -E" async{ my$s=new IO::Socket::INET('localhost:1025') or warn $^E, return; say $s 'Hello'; print scalar <$s>; shutdown $s, 2; close $s; }->detach for 1 .. 1e6"

      But as with all forking servers, it is ridiculously resource intensive for an echo daemon.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

        This is the simplest usage of IO::Select I can produce. It too, on Linux anyways, is very solid. I can kill -9 clients, and the server dosn't get hung. What do you have to do to make your symptoms appear using this script? You know I'm sure, that multiplexing with IO::Select is only meant for use with short messages; if you are doing large data transfers, you need to use fork or threads. This thread could be also be improved to use sysread instead of <>, but it generally works fine as is.
        #!/usr/bin/perl use IO::Socket; use IO::Select; my @sockets; my $machine_addr = 'localhost'; $main_sock = new IO::Socket::INET(LocalAddr=>$machine_addr, LocalPort=>1200, Proto=>'tcp', Listen=>3, Reuse=>1, ); die "Could not connect: $!" unless $main_sock; print "Starting Server\n"; $readable_handles = new IO::Select(); $readable_handles->add($main_sock); while (1) { ($new_readable) = IO::Select->select($readable_handles, undef, undef +, 0); foreach $sock (@$new_readable) { if ($sock == $main_sock) { $new_sock = $sock->accept(); $readable_handles->add($new_sock); } else { $buf = <$sock>; if ($buf) { print "$buf\n"; my @sockets = $readable_handles->can_write(); #print $sock "You sent $buf\n"; foreach my $sck(@sockets){print $sck "$buf\n";} } else { $readable_handles->remove($sock); close($sock); } } } } print "Terminating Server\n"; close $main_sock; getc();

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-12-26 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls