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!


The way forward always starts with a minimal test.

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
    Thank you both!

    I have edited my code (and finally my whole post) to reflect your comments AnonomousMonk. In doing so my error did not reproduce.

    I will look into MCE, thank you.

    I don't see where I used a variable as a variable name. I'll look again tomorrow. It is late here, and I have to get off the computer.

    Regards,

      Hi,

      "I don't see where I used a variable as a variable name."

      That's what you do when you use a symbolic reference like $$ref.

      Hope this helps!


      The way forward always starts with a minimal test.