Hi again,

Here is the same thing using MCE::Hobo. Similar code, but processes instead.

use warnings; use feature 'say'; use MCE::Hobo; use MCE::Shared; # use IO::Handle; # <-- loaded automatically by MCE and MCE::Shared:: +Server my $q = MCE::Shared-> queue; my @gang = map mce_async( sub { while ( defined( my $f = $q-> dequeue )) { require Compress::Zlib; say MCE::Hobo-> tid; } }), 1 .. 100; $q-> enqueue( $_ ) for 1 .. 100; $q-> end; $_-> join for @gang;

For modules not multi-process safe, another thing one can do on Unix platforms is having MCE::Hobo default to posix_exit to avoid all END and destructor processing.

use warnings; use feature 'say'; use MCE::Hobo; use MCE::Shared; # use IO::Handle; # <-- loaded automatically by MCE before spawning MCE::Hobo->init( posix_exit => 1 ); my $q = MCE::Shared-> queue; my @gang = map mce_async( sub { while ( defined( my $f = $q-> dequeue )) { require Compress::Zlib; say MCE::Hobo-> tid; } }), 1 .. 8; $q-> enqueue( $_ ) for 1 .. 8; $q-> end; $_-> join for @gang;

Note that the posix_exit option is not recommended if constructing an object inside the worker involving a temp file. In that case one may want the worker to exit normally. Anyway, the posix_exit option is there if needed as a last resort.

Taken from the MCE::Hobo manual: Set posix_exit to avoid all END and destructor processing. Constructing MCE::Hobo inside a thread implies 1 or if present CGI, FCGI, Coro, Curses, Gearman::Util, Gearman::XS, LWP::UserAgent, Mojo::IOLoop, Prima, STFL, Tk, Wx, or Win32::GUI.

A lot of modules are not multi-process safe and the reason for setting to 1 automatically. Btw, Prima is now multi-process safe recently.

Kind regards, Mario


In reply to Re^4: Why isn't this code thread-safe? (MCE!) by marioroy
in thread Why isn't this code thread-safe? (Is "require" thread-safe??) by vr

Title:
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, details, 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, summary, 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.