Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello Sascha2018,

See IO::FDPass for passing a handle to a process. Another way is storing the handle into a hash and use $ident as the key. I'm passing $dent to $pm->start($ident). That sets the identification for the process. Inside the on_finish block, remove the entry from the hash.

Well, something like the following. Please adjust the _uid function accordingly, to your specification.

#!/usr/bin/perl use strict; use warnings; use feature 'state'; no strict 'refs'; use IO::Socket; use IO::Select; use Parallel::ForkManager; my $die = 0; my %lkup; sub _uid { state $uid = 0; $uid = 1 if ++$uid > 2e9; $uid; } $SIG{INT} = sub { $die = 1; exit; }; my %clients = (); my $select = IO::Select->new; my $server = IO::Socket::INET->new( LocalHost => 'localhost', LocalPort => 2222, Proto => 'tcp', Listen => 10000, Reuse => 1, ) or die "Sock Error: $!\n"; my $pm = Parallel::ForkManager->new(10); $pm->set_waitpid_blocking_sleep(0); $pm->run_on_finish( sub { my ($pid,$exitcode,$ident,$exitsignal,$coredump,$get) = @_; $clients{$ident}->{ident} = $ident; $clients{$ident}->{ip} = $get->{ip}; $clients{$ident}->{socket} = delete $lkup{$ident}; }); $server->autoflush(1); $select->add($server); while (!$die) { foreach my $key ($select->can_read()) { # foreach if ($key eq $server) { # if $bay eq $server next if $key eq ""; my $bay = $server->accept or next; my $ip = $bay->peerhost(); my $ident = _uid(); $lkup{$ident} = $bay; $select->add($bay); $pm->start($ident) and next; select($bay); $| = 1; foreach my $client (keys %clients) { print "Connection from $clients{$client}->{ip} with Id +ent $clients{$client}->{ident} and Socket $clients{$client}->{socket} + ... OK\n"; } $pm->finish(0, { ip => $ip }); } } $pm->wait_all_children; } sub sendHeader { my $sock = shift; my $header =<<"EOT"; HTTP/1.0 200 OK Content-type: text/html EOT $sock->syswrite($header); }

Regards, Mario

In reply to Re: Parallel::ForkManager or something like that? by marioroy
in thread Parallel::ForkManager or something like that? by Sascha2018

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (9)
    As of 2021-04-23 11:28 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found