http://qs321.pair.com?node_id=1230172


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

I wonder what you really want to do. As far as I understood you want to read from a file from two different processes, right? The use of <DATA> is just a simplification.

Please correct me if i‘m wrong.

You may consider to use MCE::Hobo. If you don‘t use threads it forks.

Please see Re: Parallel trigger of different subs as well as the whole thread ibidem for inspiration.

Minor update: Fixed typo.

Best Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Replies are listed 'Best First'.
Re^2: How can I read DATA in parent and child?
by marioroy (Prior) on Feb 20, 2019 at 03:05 UTC

    Hi karlgoethebier,

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

    Update: Currently, the following demos work 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 works similarly with threads and MCE::Hobo.

    threads demonstration

    use strict; use warnings; use threads; use MCE::Shared; mce_open my $shared_fh, '<', \*DATA; async { print "Start Thread\n"; while (<$shared_fh>) { print "T: $_"; sleep 1; } print "Stop Thread\n"; }; print "Start Parent\n"; while (<$shared_fh>) { print "P: $_"; sleep 1; } print "Stop Parent\n"; $_->join for threads->list; __DATA__ a b c d e f g h i j

    MCE::Hobo demonstration

    use strict; use warnings; use MCE::Hobo; use MCE::Shared; mce_open my $shared_fh, '<', \*DATA; mce_async { print "Start Child\n"; while (<$shared_fh>) { print "C: $_"; sleep 1; } print "Stop Child\n"; }; print "Start Parent\n"; while (<$shared_fh>) { print "P: $_"; sleep 1; } print "Stop Parent\n"; MCE::Hobo->waitall; __DATA__ a b c d e f g h i j

    Regards, Mario

      Thank you Mario. Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help