Skip to content

Instantly share code, notes, and snippets.

@dwburke
Created February 6, 2017 20:13
Show Gist options
  • Save dwburke/e2de16038a57b7040ab55cf77fe7a337 to your computer and use it in GitHub Desktop.
Save dwburke/e2de16038a57b7040ab55cf77fe7a337 to your computer and use it in GitHub Desktop.
Simple fork job queue with AnyEvent fork_call
#!/usr/bin/env perl
use strict;
use AnyEvent;
use AnyEvent::Util;
my $cv = AnyEvent->condvar;
$AnyEvent::Util::MAX_FORKS = 5;
my $fork_jobs = 0;
for (my $i = 0; $i < 20; $i++) {
$fork_jobs++;
fork_call {
# your job here
return $$;
} sub {
($a) = @_;
print $a . "\n"; # your result output here
$cv->send if $fork_jobs == 0;
};
}
$cv->wait;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment