473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Putting function references in a Queue

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?

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?

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.

--
Richard
Aug 6 '05 #1
4 1510
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?
It would simply be the entire function object (unless you choose it
otherwise).
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?


Functions are objects that carry around their code but also their global
namespace (including modules) and some other things.

With a function f, try for instance 'dir(f)' and 'f.func_globals'.
Aug 6 '05 #2
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/
Aug 6 '05 #3
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?
It's a Python-internal-reference-thingy.
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?

Yes, generally true. It has to be implemented somewhere in the
process; the reference-thingy knows where.
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.


I think that's a good thing to do. The tricky part is getting an
event loop to wait on both the queue and other kinds of events.
Periodic polling works, but kind of sucks.

What's the 'done' argument? A lock maybe?
--
--Bryan
Aug 9 '05 #4
On Tue, 09 Aug 2005 23:46:22 GMT, Bryan Olson wrote:
I think that's a good thing to do. The tricky part is getting an
event loop to wait on both the queue and other kinds of events.
Periodic polling works, but kind of sucks.

What's the 'done' argument? A lock maybe?


'done' is just a boolean for the child thread to indicate it has finished.
The main thread keeps a dictionary of references to the child threads so
the app can send then 'abort' messages via their input Queues. When a child
thread sets 'done' to True, the main thread deletes its reference from the
dictionary.

Periodic polling triggered by a timer in the X-app main loop was the only
way I could think of checking for feedback from the child threads, not very
neat though...

Thanks to everyone for their input!

--
Richard
Aug 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: martin | last post by:
Just the simplest invocation with hard coded parameters, followed by echoing a message, hangs for exactly 1 minute. ("defaulttimeout" in php.ini is set to 30 (seconds)). If I don't wait, and...
1
by: Markus von Ehr | last post by:
Hi, in a data acquisition program I have several threads, one thread grabbing images and another doing some processing. I'd like to make my own image class containing the image and additional...
6
by: Richard Berg | last post by:
Hello, I need implementation advice: I am writing a program that should call some API functions in a specific order. The APIs are for another application, not Windows APIs. The order and type...
8
by: DanielEKFA | last post by:
Hmm... Not sure how to crack this one. I have this code: typedef bool execFunctionType(const commandDataType&); struct commandDataType { SymbolSequence Sequence; string command;...
36
by: danielx | last post by:
At first I was going to post the following: <!-- beginning of my original post --> I just discovered the inspect module, which contains the isfunction and ismethod functions. For some reason,...
5
by: Paul Sijben | last post by:
All, in a worker thread setup that communicates via queues is it possible to catch exceptions raised by the worker executed, put them in an object and send them over the queue to another thread...
6
by: rh0dium | last post by:
Hi Experts!! I am trying to get the following little snippet to push my data to the function func(). What I would expect to happen is it to print out the contents of a and loglevel. But it's...
10
by: rh0dium | last post by:
Hi all, Below is a basic threading program. The basic I idea is that I have a function which needs to be run using a queue of data. Early on I specified my function needed to only accept basic...
2
by: Jolie Chen | last post by:
I am learning template programming now, and I wrote the following code #include <iostream> #include "Queue.h" using namespace std; template <typename Tclass QueueItem { public: QueueItem(T...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.