Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to pass AnyEvent socket handle to pre-forked child process?

by michaelfung (Novice)
on Jan 21, 2013 at 03:33 UTC ( #1014374=perlquestion: print w/ replies, xml ) Need Help??
michaelfung has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I need to handle thousands of concurrent TCP connections. I use AnyEvent/EV. In order to take advantage of multiple CPUs, I pre-forked some children and pass them client socket handles. I pass the handles with AnyEvent::Beanstalk, when the child receives it, it complains it is not an AnyEvent Handle. I tried to use Storable to serialize the handle before passing but to no avail.

So, is this pre-fork approach practical at all with perl?

Comment on How to pass AnyEvent socket handle to pre-forked child process?
Re: How to pass AnyEvent socket handle to pre-forked child process?
by zwon (Monsignor) on Jan 21, 2013 at 12:05 UTC

    You can't pass filehandles between processes. Instead just let children accept connections.

      Actually, you can pass filehandles between processes, it's just not something that solves many problems, and there usually are better ways to accomplish the same, as you outlined already. :-)

      PPerl has code for passing around filehandles in pass_fd.c, and I wrote a replacement for that for Win32, but can't find it on RT. Maybe I forgot to open a bug for it.

      This approach still leaves systems not so close to Unix in the rain of course, so your better, and common, approach of using accept still stands.

        That's interesting, probably it's time to reread APUE, thank you

      I can't use accept() as it is not accessible in AnyEvent.

        If whatever you receive actually is a filehandle, or even better, a socket, then you need to maybe wrap it with an AnyEvent::Handle. Looking at AnyEvent::Beanstalk, I don't see how to pass sockets around with it though.

        You can use accept, you just should ensure that it will not block. Make server socket non-blocking, add read watcher to it, in read callback accept all connections. Something like:

        use AnyEvent::Util qw(fh_nonblocking); ...; fh_nonblocking $srv_sock, 1; my $w = AE::io $srv_sock, 0, sub { while(accept my $cli_sock, $srv_sock) { # initialize watchers for client socket } };

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2014-09-19 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (138 votes), past polls