Putting function references in a Queue

Benjamin Niemann pink at odahoda.de
Sat Aug 6 08:53:22 EDT 2005


Richard Townsend wrote:

> I've been experimenting putting a reference to a function into a Queue
> object and was wondering what actually gets put in the Queue - is it the
> function's code object?

No, it's justa referenceto the function object.
 
> If I read from the Queue in a different module, it appears that I don't
> need to import the module that defines the function - or any module that
> it uses - is this generally true, or are there some conditions to be aware
> of?

There function reference is sufficient to call the function, there's not
need to import the module containing the function. It has already been
imported (otherwise the function wouldn't be there) and the function has a
reference to this module in order to resolve its global (module-level)
references.

> The scenario I'm working on has child threads doing some tasks and then
> sending back tuples (done, function, args, kwargs) via the Queue, to be
> called in the main thread. The Python code is ultimately embedded in a
> C/Motif app.

As long as you take care of the usual threading issues (concurrent access to
shared objects guarded by semaphores etc.), there should not be any greater
problems.
The function will be executed in the thread that is called it of course (and
not in the thread that sent the reference to it).

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list