executing multiple functions in background simultaneously
cs at zip.com.au
Thu Jan 15 05:33:06 CET 2009
On 14Jan2009 17:11, Catherine Moroney <Catherine.M.Moroney at jpl.nasa.gov> wrote:
> Cameron Simpson wrote:
>> On 14Jan2009 15:50, Catherine Moroney <Catherine.M.Moroney at jpl.nasa.gov> wrote:
>>> James Mills wrote:
>>>> On Wed, Jan 14, 2009 at 11:02 AM, Catherine Moroney
>>>> <Catherine.M.Moroney at jpl.nasa.gov> wrote:
>>>>> I would like to spawn off multiple instances of a function
>>>>> and run them simultaneously and then wait until they all complete.
>>>> Try using the python standard threading module.
>>>> Create multiple instances of Thread with target=your_function
>>>> Maintain a list of these new Thread instnaces
>>>> Join (wait) on them.
>>> What is the proper syntax to use if I wish to return variables
>>> from a function run as a thread?
>> The easy thing is to use a Queue object. The background thread uses
>> .put() to place a computed result on the QUeue and the caller uses
>> .get() to read from the queue. There's an assortment of other ways too.
> Thank you for this hint. This goes a long way to solving
> my problem.
> One question - is there any way to name the objects that get
> put on a queue? For my application, it's important to know
> which thread put a particular item on the queue.
Give each thread an id (eg give it a name when you create it, and keep
track). Instead of going:
and at the main program:
threadname, value = queue.get()
You're still put()ing a single value, it just happens to be a tuple
of (threadname, value).
You don't even need to use a name (and thus need to backtrack through
some name->object dictionary). If every thread has some object for its
state you can use the object instead of the threadname. Then you've got
direct access to the thread object.
Cameron Simpson <cs at zip.com.au> DoD#743
More information about the Python-list