Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Threads From Hell #1: How To Share A Hash [SOLVED]

by marioroy (Prior)
on Aug 29, 2015 at 04:49 UTC ( [id://1140396]=note: print w/replies, xml ) Need Help??


in reply to Threads From Hell #1: How To Share A Hash [SOLVED]

I had always wanted to share a solution using MCE which comes with a sequence generator. This allows one to use a pool of workers seamlessly while iterating through a sequence of numbers.

The first example is a modified version of the demonstration by BrowserUk.

use strict; use threads; use threads::shared; use MCE::Flow; use Math::BigInt; use feature qw(say); my %result : shared; mce_flow_s { chunk_size => 1, max_workers => 4 }, \&process, 1, 1000; for my $key ( sort{ $a <=> $b } keys %result ) { say "$key => ", $result{ $key }->bstr; } sub process { my $number = $_; my $factorial = factorial($number); $result{$number} = shared_clone( $factorial ); ## clone the object } sub factorial { my $number = shift; Math::BigInt->bfac($number); }

The next example requires the upcoming MCE 1.7 release, currently residing in GitHub. Notice the use of MCE::Shared and the capital letter S in Shared for the variable attribute.

use strict; use MCE::Flow; use MCE::Shared; use Math::BigInt; use feature qw(say); my %result : Shared; mce_flow_s { chunk_size => 1, max_workers => 4 }, \&process, 1, 1000; for my $key ( sort{ $a <=> $b } keys %result ) { say "$key => ", $result{ $key }->bstr; } sub process { my $number = $_; my $factorial = factorial($number); $result{$number} = $factorial; # the object is shared automatical +ly } sub factorial { my $number = shift; Math::BigInt->bfac($number); }

Both complete in the same time frame. Threads is not required to run MCE::Shared as it supports threads and processes alike. Thus, one may include threads if desired.

Init is another way to specify MCE options.

MCE::Flow::init { chunk_size => 1, max_workers => 4, }; mce_flow_s \&process, 1, 1000;

Kind regards, Mario

Replies are listed 'Best First'.
Re^2: Threads From Hell #1: How To Share A Hash [SOLVED]
by karlgoethebier (Abbot) on Aug 29, 2015 at 18:21 UTC

    Thank you very much marioroy for sharing this! Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1140396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found