The problem here is that print is not atomic ... Here's a script that provokes it:
Hm. And the fix is sooo complicated:
use strict; use threads; use threads::shared; my $sem :shared; open my $fh, '>', 'outfile' or die $!; my $th = 0; my @threads = map { $th++; async( sub { sleep(1); for(1 .. 30_000) { lock $sem; print $fh "Th +read $th\n" } } ); } (1 .. 500); $_->join foreach @threads; close $fh;
A whole 3 lines.
Regarding modules to facilitate the implementation of said state machine, one I found easy to use (actually the only one I've ever used in production code) is POE::Component::Client::UserAgentPOE::Component::Client::HTTP. (edited, it's been a while but the name didn't sound quite right) POE is rather heavyweight though (not that it mattered much here) so AnyEvent::Curl::Multi might be worth a look too.
Okay, so where's the code? How about you run what you brung?
Betcha don't!
And if you do, betcha it takes you 10 times longer to write; requires 10 times as much (user) code; requires 20 times as many support modules that accumulate to be 30 times as much actual non-core code to trust the authors of and require outside support for when it goes wrong; and finally, runs slower and less efficiently than the 5 minute-to-write, 30 line threaded script above.
In reply to Re^7: Consumes memory then crashs
by BrowserUk
in thread Consumes memory then crashs
by allhellno
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |