Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Threads memory consumption

by mojo2405 (Acolyte)
on Dec 05, 2013 at 15:07 UTC ( #1065771=note: print w/ replies, xml ) Need Help??


in reply to Re: Threads memory consumption
in thread Threads memory consumption

my need is to return data from each thread / fork child. it can be hash / object / string or whatever . This is the reason I'm using threads instead of forks. As far as I know - there is no way returning data from fork child.. only with threads... am I correct ?


Comment on Re^2: Threads memory consumption
Re^3: Threads memory consumption
by zwon (Monsignor) on Dec 05, 2013 at 15:36 UTC
    It is possible to pass result to parent process, and is quite easy. You can use Parallel::ForkManager for example:
    use 5.010; use strict; use warnings; use Parallel::ForkManager; use Data::Printer; my $pm = Parallel::ForkManager->new(2); $pm->run_on_finish( sub { # result from the child will be passed as 6th arg to callback my $res = $_[5]; p $res; } ); for (1..3) { $pm->start and next; # from here and till $pm->finish child process running # do something useful and store result in $res my $res = { aaa => $_ }; # this will terminate child and pass $res to parent process $pm->finish(0, $res); } $pm->wait_all_children;
      Thanks alot ! used this method - and everything works for me great now. I have a question about this method - Can I have some shared data between the child forks ?
        Yes you can, see IPC::Shareable and IPC::ShareLite, but I don't recommend you to use these -- refactor your program so it wouldn't require shared variables, or use some message passing module instead.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-07-31 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls