bennymack has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Event based handling of multiple connections with AnyEvent
by rcaputo (Chaplain) on Oct 29, 2008 at 00:29 UTC | |
|
Re: Event based handling of multiple connections with AnyEvent
by Anonymous Monk on Oct 29, 2008 at 03:56 UTC | |
by PEVANS (Initiate) on Oct 30, 2008 at 13:39 UTC | |
|
Re: Event based handling of multiple connections with AnyEvent
by bennymack (Pilgrim) on Nov 04, 2008 at 12:10 UTC |