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

Parallel::ForkManager question

by crackotter (Beadle)
on Jun 12, 2003 at 22:44 UTC ( [id://265529]=perlquestion: print w/replies, xml ) Need Help??

crackotter has asked for the wisdom of the Perl Monks concerning the following question:

Hola Monks, I ealier had a question related to threads, where somebody recommend I use forks instead. Another person recommended I use the Parallel::ForkManager module. I got a test script up and running with it, (very easy) but I do not how to get the return values. Please help! :)
#!/usr/bin/perl #Script to test out the Parallel::ForkManager module use strict; use Parallel::ForkManager; %Test_hash=("A",1,"B",2,"C",3,"D",4); #Start the Parrel Fork Manager my $pfm = new Parallel::ForkManager($max_threads); # Setup a callback for when a child finishes up so we can # get it's exit code $pfm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; print $pid . "\n"; # print "** $ident just got out of the pool ". # "with PID $pid and exit code: $exit_code\n"; } ); my $line=""; my $child=""; my %Values=(); foreach $line(keys %Test_hash) { # Forks and returns the pid for the child: my $pid = $pfm->start($line) and next; $Values{$pid}=&Test_fork($line, $Test_hash{$line}); $pfm->finish($child); # Terminates the child proces } sub Test_function { my ($a,$b)=@_ my $c = $a * $b; return $c; }
How do I get the return values of the Subfunction I fork off?? In the $pfm->run_on_finish section??

Replies are listed 'Best First'.
Re: Parallel::ForkManager question
by Mr_Person (Hermit) on Jun 13, 2003 at 00:27 UTC

    Getting values back from forked programs can be tricky because when a program is forked, its child process is completely seperate (well, mostly) from the parent process - it just happens to be identical to it at first. Because of this, any values that you change in the child process are lost when it exits unless you somehow get them back to the parent process.

    There are several ways you can do this, the easiest would be to use the exit code. However, if I remember correctly, exit codes can only be integers from 0 to 255, so that limits how much information you can pass back. Another option would be to use some form of shared memory, but that can be difficult to program. You could also have all the processes write to a file, but you should make sure to have each one obtain an exclusive lock on the file before writing to it, then close it to prevent a race condition between all the processes that are going to be using it.

    Probably one of the nicest ways is to use a database. The database won't have any problem accepting connections from many multiple clients and it will take care of locking issues for you as well. The only problem is a speed tradeoff, as opening a connection to the database will take a little bit.

    Just thought of another one, you could use something like syslog if you're just writing out the result of an operation.

    Hope that helps!

Re: Parallel::ForkManager question
by perrin (Chancellor) on Jun 13, 2003 at 14:32 UTC
    Did you read the link I gave you in the last thread? It contains working code for this, although it doesn't use the module you're asking about.
Re: Parallel::ForkManager question
by BrowserUk (Patriarch) on Jun 13, 2003 at 01:44 UTC

    It's exactly this process of passing data between the processes that threads solves. See my reply to your original query atRe: Thread Advice. If you strip the comments, it is barely any more complicated than you code here, and it provides for reliable bi-directional communication by using Thread::Queue.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found