my $complex_data_structure = {'a'=>[1,2,3], 'b'=>{'c'=>[4,5,6],'d'=>LWP::UserAgent->new()}};
my $serialised_data = Sereal::Encoder::encode_sereal($complex_data_structure);
$pfm->finish(0, \$serialised_data); # <<< note that we pass a reference to our serialised-data.
####
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
my $data = Sereal::Decoder::decode_sereal($$data_structure_reference); ## de-referencing the ref to the serialised data and then de-serialising.
####
#!/usr/bin/env perl
use strict;
use warnings;
use Parallel::ForkManager;
use Data::Dump qw/dump/;
# bliako
use Sereal::Encoder qw(encode_sereal sereal_encode_with_object);
use Sereal::Decoder qw(decode_sereal sereal_decode_with_object);
my @names = ();
my %list = ();
my %thing = ();
my $threads = 20;
my $pfm = new Parallel::ForkManager( $threads );
my %results = ();
$pfm->run_on_finish( sub {
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
my $data = Sereal::Decoder::decode_sereal($$data_structure_reference);
# surely this is sequential code here so no need to lock %results, right?
$results{$pid} = $data;
# using pid as key is not a good idea because maybe a pid number will be eventually recycled.
});
my $things_hr = {
'job1' => 'this is job 1 data',
'job2' => 'this is job 2 data',
'job3' => 'this is job 3 data',
'job4' => 'this is job 4 data',
'job5' => 'this is job 5 data',
};
THELOOP:
foreach my $thing(keys %{$things_hr}) {
print "THING = $thing\n";
$pfm->start and next THELOOP;
my $pid = $$;
my $returned_data = {
'item1' => "item1 from pid $pid, for item $thing and value ".$things_hr->{$thing},
'item2' => "item2 from pid $pid, for item $thing and value ".$things_hr->{$thing},
"item3 are some array refs for pid: $pid", => [1,2,3,4],
};
my $serialised_data = Sereal::Encoder::encode_sereal($returned_data);
print "pid=$pid, this is what I am sending:\n".dump($returned_data)."\n";
$pfm->finish(0, \$serialised_data);
}
$pfm->wait_all_children;
print "Here are the results:\n".dump(%results)."\n";