Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Parallel:ForkManager how to pass back a list

by davido (Cardinal)
on Apr 05, 2024 at 18:04 UTC ( [id://11158704]=note: print w/replies, xml ) Need Help??


in reply to Parallel:ForkManager how to pass back a list

It is an actual data structure reference. I have this in some of my code somewhere:

$pm->run_on_finish( sub { $buckets->{$_[0]} = $_[1] for @{$_[5]}; } );

So that means that $_[5] ($data_structure_reference) is shaped something like:

$data_structure_reference = [ ['a', 1], ['e', 1], ['l', 1], ['p', 2], ]

Keep in mind the following information from the POD for Parallel::ForkManager:

The ability for the parent to retrieve data structures is new as of version 0.7.6.

Each child process may optionally send 1 data structure back to the parent. By data structure, we mean a reference to a string, hash or array. The contents of the data structure are written out to temporary files on disc using the Storable modules' store() method. The reference is then retrieved from within the code you send to the run_on_finish callback.

The data structure can be any scalar perl data structure which makes sense: string, numeric value or a reference to an array, hash or object.

There are 2 steps involved in retrieving data structures:

1) A reference to the data structure the child wishes to send back to the parent is provided as the second argument to the finish() call. It is up to the child to decide whether or not to send anything back to the parent.

2) The data structure reference is retrieved using the callback provided in the run_on_finish() method.

Keep in mind that data structure retrieval is not the same as returning a data structure from a method call. That is not what actually occurs. The data structure referenced in a given child process is serialized and written out to a file by Storable. The file is subsequently read back into memory and a new data structure belonging to the parent process is created. Please consider the performance penalty it can imply, so try to keep the returned structure small.

In the case of my code example, the data structure is being layered into a hash that has been in scope since before the forked children were created.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2025-12-15 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (95 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.