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

I am OOP challenge, and cannnot get my script to work. I am converting a single threaded operation into multi-threaded using Parallel::ForkManager. It SEEMED so simple.. Here is essentially how the the script used to work:
OLD VERSION
if ($a) { foreach $a (@a) { print $a; discover ($a); } } if ($b) { print $b: discover ($b); } sub discover { ## do lots of repetitive stuff }
So, now I have it doing:
NEW SPIFFY VERSION
if ($a) { my $manager = new Parallel::Forkmanager( 10 ); foreach $a (@a) { $manager->start and next; print $a; discover ($a); $manager->finish; } if (b) { discover (b); sub discover { do lots of repetitive stuff }
So, obviously calling a function from within this manager construct must not be how to do it, because "she ain't workin'". I am not sure if I'm calling it from with an object, or a method, or what.. it makes it significantly harder to find something when you don't know what to call what you are looking for. Thank you in advance for any help.

Replies are listed 'Best First'.
Re: Calling a sub routine from an object
by tirwhan (Abbot) on Jan 05, 2006 at 09:55 UTC

    Is that "she ain't working" as in "Some output I'm expecting isn't appearing" or "My b**** ain't bringin' in da money" ;-)?. IOW, what exactly are you expecting to happen that isn't happening? I can see one obvious problem with your second version (you use "b" instead of "$b"), but I guess that's just a typo in the example you gave. Other than that you need to be aware that the children generated by Parallel::Forkmanager do not share state, so if you're manipulating some internal variable in one child the other children won't see the change.


    A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox
      Oh, that was indeed a typo. I was trying to figure out HOW to call a sub. By not working, it doesn't run the sub. It runs the "print statement" perfectly, but the major functionality NEVER happens.

        I can't see anything wrong with the way you're calling the sub (this doesn't really have anything to do with OO btw, the ForkManager is OO but your code just uses it normally). If you say that $a is being printed, then I suspect something is going wrong in the sub. Could you try to implement a minimum piece of real code which can be run on it's own and illustrates your problem (i.e. strip out anything that's not necessary to make it fail)? That will make it possible for us to find out what's going wrong.

        Wild stab in the dark: I think this is again just a typo, but to be sure, you have:

        if (b) { discover (b); sub discover { do lots of repetitive stuff }

        Obviously "b" should be "$b" as mentioned before, but you also have a right brace missing:

        if ($b) { discover ($b); } sub discover { # do lots of repetitive stuff }
        As I said, I guess that won't be the problem, just double-checking.

        There are ten types of people: those that understand binary and those that don't.
Re: Calling a sub routine from an object
by Anonymous Monk on Jan 05, 2006 at 19:51 UTC
    You are aware that multi-threaded and Parallel::ForkManager aren't exactly synonymous. Multi-threaded tends to mean one process, actual threads, and data is shared between all of the threads. P::F uses a new process for each instance through the loop (hence *Fork*Manager). Actions in one process do necessarily affect another -- say if 'discover' goes and finds something and saves the data in a global array. Put a print statement as the operation in 'discover' and you may well find that it is indeed being run, and that it's just not doing what you think it's supposed to be doing.