http://www.perlmonks.org?node_id=873284

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

Hello PerlMonks,
In my script I am using threads and Thread::Queue to distribute work between child threads. But some of the methods in the modules I load initially in the script cannot be accessed by my child threads.

use threads; use Thread Queue; use Calculate; use Remediate; Here I create child threads. gather work for child threads and send it to Queue when child pick's up work.. Inside sub child() it can't access some me +thods from Calculate and Remediate modules. it gives me error like below. Operation """": no method found, argument in overloaded package main a +t /home/rpotter/Report.pl
How can I make the methods inside the modules I initally loaded accessible to both parent and child threads?

Replies are listed 'Best First'.
Re: how to make a module and its methods accessable between paren t and child threads
by BrowserUk (Patriarch) on Nov 23, 2010 at 20:55 UTC
Re: how to make a module and its methods accessable between paren t and child threads
by ruoso (Curate) on Nov 24, 2010 at 12:05 UTC

    The trick I usually do in such cases is to send the object serialized (with Storable freeze) through the queue. That, obviously, means the objects represent more a IPC then actual memory sharing, but you didn't give enough information to know if serializing the values is appropriate.

    Note that threads in Perl are not like threads in C, or Python, or Java. In all those languages, all threads share the memory with every other thread (note that python gives up real concurrency to do that). In Perl, you can only share specific data. And there are a set of limits on how the shared data should look like.

    This makes threads in Perl unbearable for a lot of scenarios, others simply get much harder, but the bright side is that Perl has real concurrency (while Python has a Global Interpreter Lock). But usually, in Perl, when you want to use threads, you really have to design your app with that in mind, you can't just parallellize a small part of the code.

    daniel
Re: how to make a module and its methods accessable between paren t and child threads
by Anonymous Monk on Nov 23, 2010 at 21:20 UTC

    Could you go ahead and elaborate a little bit on the alternative you have in mind, anyway?   Just so that someone who stumbles upon this thread would leave with a complete thought ...

      Could you go ahead and elaborate a little bit on the alternative you have in mind, anyway?

      Assuming this is addressed to me rather than the OP...

      It is hard to say much more than I already did in my post above without a much clearer picture of what the OP's code does. The first thing I'd need is a description of why the OP is using threading? What benefits is s/he hoping to derive from their use? Is this an attempt to gain performance through the parallelisation of CPU-intensive code? Or allowing one part of the program to continue whilst another waits on IO?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I used threads to gain performance by allowing one part of the program to continue while another waits on IO.