473,785 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1526
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
6516
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 immediately hit the browser's "stop" button I get that "transfer interrupted" message, but the email still gets sent and received just fine. Sendmail is not running, "sendmail_path" is not set in php.ini, and the email headers look as if the email was...
1
2173
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 information (a lot of data, some Megabytes). Then I'd like to transfer this object to another thread. I tried to transfer an image over a queue and it works! (see code below)
6
6079
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 of the calls is determined at runtime. There are a couple of hundred of those APIs that can be called, and they all take different parameters. What I need is somehow queue the calls with their parameters and then dequeue and execute them one by...
8
1461
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; execFunctionType* executer; };
36
1882
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, I used to be under the impression that Python methods are no different from Python functions. Naturally, I wondered why both of these needed to exist (I also later discovered the isroutine function). So I started to experiment at prompt. Here's...
5
3567
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 where the exception is raised in that scope? considering that an exception is an object I feel it ought to be possible, however I do not see how to go about it.
6
1352
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 not working. Can someone please help me out. ---------<snip>-------------- #!/usr/bin/env python
10
1597
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 parameters ( no postional *args or *kwargs ) but now I am re-writing it and I want to accept these. Is there anyway to determine what parameters are needed by a given function and format the arguments given appropriately. My problem is that I...
2
1989
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 item):value(item),next(0){}
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.