in reply to Parallel::ForkManager problem

The call to $pm->start creates a new process with it's own memory address space, variables are not shared between processes. So once you have 10 processes started you have 11 versions of the hash %result in memory, each of them addressable only from the process it belongs to. The one you access outside the loop belongs to the parent process and is never modified.

If you want to use a shared variable you may want to consider using threads instead of processes. Alternatively, use one of the forms of interprocess communication described in perldoc perlipc or search CPAN for "IPC" or "shared".


All dogma is stupid.

Replies are listed 'Best First'.
Re^2: Parallel::ForkManager problem
by salva (Canon) on Mar 28, 2006 at 10:49 UTC
    in the OP example, using fork is still a valid option because the exit code can be used to pass an integer result back to the parent.

      Yeah,though to me that has always seemed a bit of a hackish way of doing things. Exit codes should communicate error conditions or success of a process, if you want to pass regular information between child and parents use IPC (it's not like that's really so much more difficult than setting up the exit code handler). YMMV of course.


      All dogma is stupid.