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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |