Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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?

Replies are listed 'Best First'.
Re: How to pass AnyEvent socket handle to pre-forked child process?
by zwon (Abbot) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1014374]
Approved by Corion
[Tux]: so, another app with gtk GUI had something similar, but it defined the IM_MODULE(s) as cedilla.
[Tux]: when I set QT_IM_MODULE and GTK_IM_MODULE both to c<xim> and *unset* XMODIFIERS, all works fantastic
Tux => $HOME
[Tanktalus]: great, now I'm going to have to convert html codes to text for display... :)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2017-09-25 21:14 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (291 votes). Check out past polls.