Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How can I read DATA in parent and child?

by marioroy (Priest)
on Feb 20, 2019 at 02:50 UTC ( #1230210=note: print w/replies, xml ) Need Help??


in reply to How can I read DATA in parent and child?

Hi Skeeve,

Update: Ah, the OP wants to read DATA independently. See tybalt89's solution.

Update: Currently, the following demo works on Unix platforms with IO::FDPass: automatically loaded by MCE::Shared if present in Perl. The next update to MCE::Shared on CPAN will support reading from the DATA handle without involving IO::FDPass and work on Windows including Cygwin.

MCE::Shared is not exclusive to only MCE parallel modules: e.g. MCE, MCE::Flow, MCE::Hobo, and etcetera. MCE::Shared also works with threads, even fork shown below, and other parallel modules found on metacpan.

Code

use strict; use warnings; use MCE::Shared; mce_open my $shared_fh, '<', \*DATA; my $pid= fork(); die unless defined $pid; if ($pid == 0) { print "Start Child\n"; while (<$shared_fh>) { print "C: $_"; sleep 1; } print "Stop Child\n"; } else { print "Start Parent\n"; while (<$shared_fh>) { print "P: $_"; sleep 1; } print "Stop Parent\n"; } __DATA__ a b c d e f g h i j

Output

Start Parent Start Child P: a C: b C: c P: d C: e P: f C: g P: h C: i P: j Stop Child Stop Parent

Regards, Mario

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2019-03-22 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (114 votes). Check out past polls.

    Notices?