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

Hello Monks, Yesterday you all were very helpful with my question about running parallel processes.

I got a working version with fork. But I can see the benefit of using the Parallel::Jobs module. But I can't get it to work. I have 2 problems:

1. I can't get a simple sample to run in parallel. (See complete example below)
I'm trying to print "Middle" before "Hello... 10 seconds."
I tested this on an NT box. Using ActiveState perl 5.6
Main start
Hello. I want a nap.
Hello. I just slept 10 seconds.
Started hello: 2352
Middle: I'm impatient, so I will print now.
'1' is not recognized as an internal or external command,
operable program or batch file.
'1' is not recognized as an internal or external command,
operable program or batch file.
Started middle: 356
356, EXIT, 256
2352, EXIT, 256
Ciao.  All the parallel parts are done.
Main end

2. I also don't understand the documentation's perl notation for sending the stdio/stderr to separate files. Here is a snippet from the documentation:
$pid = Parallel::Jobs::start_job({ stdin_file => filename | stdin_handle => *HANDLE, stdout_handle => *HANDLE | stdout_capture => 1, stderr_handle => *HANDLE | stderr_capture => 1 }, ... cmd as above ...);
Could someone please explain? :)

In case it's important. I need a cross-platform solution (NT, Linux, Solaris) and using at least perl 5.6 on NT. Here is the code:
#!/usr/bin/perl -w use strict; use Parallel::Jobs; my($pid, $pid2); print "Main start\n"; $pid = Parallel::Jobs::start_job(\&hello()); print "Started hello: $pid\n"; $pid2 = Parallel::Jobs::start_job(\&middle()); print "Started middle: $pid2\n"; my ($pid_new, $event, $data) = Parallel::Jobs::watch_jobs(); while(defined($event)) { print "$pid_new, $event, $data\n"; ($pid_new, $event, $data) = Parallel::Jobs::watch_jobs(); } ciao(); print "Main end\n"; exit 0; ################################# #subroutines ################################# sub hello{ print "Hello. I want a nap.\n"; sleep(10); print "Hello. I just slept 10 seconds.\n"; } sub middle{ print "Middle: I'm impatient, so I will print now.\n"; } sub ciao{ print "Ciao. All the parallel parts are done.\n"; }

Replies are listed 'Best First'.
Re: Problem's with Parallel::Jobs
by RMGir (Prior) on Sep 11, 2002 at 11:58 UTC
    The problem is that Parallel::Jobs::start_job takes an external program to run as an argument.

    You're passing it a reference the result of invoking hello() or middle(), a reference to 1 in both cases (the result of print).

    It looks like P::J::s_j is smart enough to dereference that scalar reference, so it tries to run the program "1", and doesn't find it...

    You probably want to read the docs again.

    The STDIN => *HANDLE notation means that you can pass an open filehandle in to the start_jobs routine, which the called program will use for input.

    The outputs can either be fed to handles of their own, OR be passed back to your program. If you choose the capture option, then you'll get the outputs as events when you watch_jobs.

    It would look like this:

    open(SCRIPT, "<", "script.txt") or die "Can't open script.txt, error $ +!"; Parallel::Fork::start_job("bc", stdin_handle => *SCRIPT, stdout_capture=>1, stderr_capture=>1);
    This would make the "bc" calculator program execute your script, and let you read the results with watch_jobs.
    --
    Mike