Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Threads From Hell #1: How To Share A Hash [SOLVED] by marioroy
in thread Threads From Hell #1: How To Share A Hash [SOLVED] by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found