Hello, Monks.

It's been a while but I've got a problem that I can't easily find the solution to for some reason. It involves event-based programming which I am just starting to wrap my head around.

I'm basically trying to write a simple test server that can allow multiple connections per process. It seems like this should be right up the alley of any of the asynchronous IO modules on CPAN. As in, it seems like I should be able to write a single process server that handles multiple long-lived requests by multiplexing the socket reads and writes.

Unfortunately I cannot get this to work. I really just need a recipe and then I can take it from there. I have an example that will respond to two persistent connections but anymore than that and the requests just are basically ignored until a process is cleared up. I probably should start with a single process example and go from there. Maybe I'm just confusing matters. It seems like it should be pretty simple.

Here's what I have so far. Please to be helping me out :)

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use POSIX; use AnyEvent; use AnyEvent::Handle; use EV; use IO::Socket::INET(); my $socket = IO::Socket::INET->new( Listen => 5, ReuseAddr => 1, LocalPort => 8888, Proto => 'tcp' ); POSIX::setsid(); for my $child_num ( 1 .. 2 ) { my $pid = fork; if( $pid == 0 ) { warn $$, ' CHILD: started'; while( my $client = $socket->accept ) { my $cv = AnyEvent->condvar; $client->autoflush( 1 ); my( $peerhost, $peerport ) = ( $client->peerhost, $client- +>peerport ); my $handle = AnyEvent::Handle->new( fh => $client, on_drain => sub { warn $$, ' drain '; }, on_error => sub { print "Client connection error: $pee +rhost:$peerport: $!\n"; $cv->broadcast; }, ); my $read; $read = sub { my( $self, $line ) = @_; warn $$, ' got line ', $line; $self->push_read( line => $read ); }; $handle->push_read( line => $read,); $cv->recv; warn $$, ' $client done.'; } warn 'CHILD: done'; exit; } } while( ( my $pid = wait ) != -1 ) { warn 'REAPED: ', $pid; } warn 'PARENT: done'; exit;

In reply to Event based handling of multiple connections with AnyEvent by bennymack

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.