in reply to [solved] Passing a sub reference which uses Parallel::ForkManager
Hi there!
You need to read and follow the now 22-yr old classic Why it's stupid to 'use a variable as a variable name'.
You would greatly benefit from learning to use MCE and MCE::Shared. Here's a trivial SSCCE example from a talk on MCE I gave recently. It shows how to pass a list of tasks to a pool of workers and have them use a shared data structure for the results. Easy enough to encapsulate into your own handler. Spend your time doing that, instead of figuring out the concurrency part!
use strict; use warnings; use 5.010; use Data::Dumper; use HTTP::Tiny; use Time::HiRes 'gettimeofday', 'tv_interval'; use MCE; use MCE::Shared; my $ua = HTTP::Tiny->new( timeout => 10 ); # carefully curated list of slow-loading websites my @urls = qw< gap.com amazon.com ebay.com lego.com wunderground.com imdb.com underarmour.com disney.com espn.com dailymail.com >; my $report = MCE::Shared->hash; MCE->new( max_workers => 6 )->foreach( \@urls, sub { my $start = [gettimeofday]; $ua->get('https://' . $_); $report->set( $_, tv_interval($start, [gettimeofday]) ); }); say Dumper $report->export;
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Passing a sub reference which uses Parallel::ForkManager
by minor_wazoo (Novice) on Apr 04, 2020 at 21:35 UTC | |
by 1nickt (Canon) on Apr 05, 2020 at 16:17 UTC |