in reply to Fork or Thread?

I can't comment on which is better (fork/threads), but here's an example using Parallel::ForkManager that I wrote a while ago for someone else.

I've modified it a bit to suit your requirements. First, $max_forks specifies the maximum number of forks to run at a time. Next, I create an array of 1000 elements. Then in the while(), I splice out 200 elements of the @array into a new array, and fork out to db_save() with that array slice, simulating your desire to only do a specific number of items per fork().

#!/usr/bin/perl use warnings; use strict; use Parallel::ForkManager; my $max_forks = 3; my $fork = new Parallel::ForkManager($max_forks); my @array = (1..1000); # on start callback $fork->run_on_start( sub { my $pid = shift; } ); # on finish callback $fork->run_on_finish( sub { my ($pid, $exit, $ident, $signal, $core) = @_; if ($core){ print "PID $pid core dumped.\n"; } } ); # forking code while (@array){ my @chunk = splice @array, 0, 200; $fork->start and next; sleep(2); db_save(@chunk); $fork->finish; } sub db_save { my @a = @_; print "$a[1]\n"; } $fork->wait_all_children;

Replies are listed 'Best First'.
Re^2: Fork or Thread?
by beanscake (Acolyte) on Jul 29, 2015 at 22:05 UTC
    thank you stevieb, this worked for me.