473,503 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading Pool Event()

Hi all

I just cannot seem to find any documentation that shows an example of using
the factory method Event() in threads. I have a thread pool and if there are
no jobs in a Queue I want them to wait for something to be inserted. When a
job is inserted I want to send an Event, the first thread that picks it up
runs with the job the rest wait for another insert Event.

I have been looking at some C, c++ implementations and some use a continious
loop within a thread that polls the queue then if one is found pops it from
the queue and runs with it.

Problem when threads are dormant is it not better having the main thread
(i.e process) sending a signal rather than 10 threads polling surely this is
cpu intensive ??

any helps or references on Event() is much appreciated

many thanks

Graeme
Jul 18 '05 #1
8 4192
In article <0t****************@nnrp1.ozemail.com.au>,
Graeme Matthew <gs*******@ozemail.com.au> wrote:

I just cannot seem to find any documentation that shows an example of
using the factory method Event() in threads. I have a thread pool and
if there are no jobs in a Queue I want them to wait for something to
be inserted. When a job is inserted I want to send an Event, the first
thread that picks it up runs with the job the rest wait for another
insert Event.


Given that you're already using a Queue, there is no, repeat NO, reason
for using an Event. Just have your threads block on the Queue.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

A: No.
Q: Is top-posting okay?
Jul 18 '05 #2
Aahz

Thanks, ive actually been using your OSCON slides which have helped a lot.
So it technically correct that all worker threads in a thread pool are
either doing some work or polling the queue to find out if there is a job
to process ?

eg: (pseudocodish :-))

def run(self):

while 1:

self.lock.acquire()

if QueueManager.HasJob:
job = QueueManager.GetFreeJob()
__processJob(job)

self.lock.release()

def __processJob(self):

blah blah blah .....
many thanks ......
"Aahz" <aa**@pythoncraft.com> wrote in message
news:bf**********@panix1.panix.com...
In article <0t****************@nnrp1.ozemail.com.au>,
Graeme Matthew <gs*******@ozemail.com.au> wrote:

I just cannot seem to find any documentation that shows an example of
using the factory method Event() in threads. I have a thread pool and
if there are no jobs in a Queue I want them to wait for something to
be inserted. When a job is inserted I want to send an Event, the first
thread that picks it up runs with the job the rest wait for another
insert Event.
Given that you're already using a Queue, there is no, repeat NO, reason
for using an Event. Just have your threads block on the Queue.
--
Aahz (aa**@pythoncraft.com) <*>

http://www.pythoncraft.com/
A: No.
Q: Is top-posting okay?

Jul 18 '05 #3
In article <ND*****************@nnrp1.ozemail.com.au>,
Graeme Matthew <gs*******@ozemail.com.au> wrote:

Thanks, ive actually been using your OSCON slides which have helped a
lot. So it technically correct that all worker threads in a thread
pool are either doing some work or polling the queue to find out if
there is a job to process ?
The whole point is that it *doesn't* poll. It blocks on a lock internal
to the Queue object. That makes it extremely efficient.
eg: (pseudocodish :-))

def run(self):

while 1:

self.lock.acquire()

if QueueManager.HasJob:
job = QueueManager.GetFreeJob()
__processJob(job)

self.lock.release()

def __processJob(self):

blah blah blah .....


Nope, it's even simpler than that:

def run(self):
done = False
while not done:
job = q.get()
if job.done:
done = True
else:
__processJob(job)

The Queue handles all the locks for you.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

A: No.
Q: Is top-posting okay?
Jul 18 '05 #4
On Fri, 2003-07-18 at 14:28, Graeme Matthew wrote:
Aahz

Thanks, ive actually been using your OSCON slides which have helped a lot.
So it technically correct that all worker threads in a thread pool are
either doing some work or polling the queue to find out if there is a job
to process ?
They don't poll the queue, they block waiting on it until something is
available.
eg: (pseudocodish :-))

def run(self):

while 1:

self.lock.acquire()

if QueueManager.HasJob:
job = QueueManager.GetFreeJob()
__processJob(job)

self.lock.release()

def __processJob(self):

blah blah blah .....


More like:

def run():
while 1:
job = queue.get() # this blocks until something is queue.put()
_processjob(job)
Acquiring the locks isn't necessary and ill-advised. The Queue itself
can be thought of as a type of locking mechanism, so you raise the
possibility of a deadlock condition by nesting locks (for instance if
you also put locks around the queue.put()).

Regards,

--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 (800) 735-0555
Jul 18 '05 #5
ok still a bit confused sorry .... first attempt at a thread pool

Am I correct in saying that each thread runs in a continious loop

each thread calls queue.get. The get method will block and not return
anything until an item is placed into it. When an item is placed into it,
one of the threads will get assinged a job i.e the first one that happens to
be in use during the cycle ?

The job is returned to the thread, it runs with the job and does all the
processing then returns and calls queue.get again and waits for a job to
become available ?

When placing a job via Queue.put() one must acquire a lock place it and then
release it

Am i aslo correct in saying that queue is actually doing the blocking and
controls which thread gets the job ?

Lastly, sorry for all the questions, surely the CPU usage is the same when
the queue is waiting for jobs and when the threads are polling as theyre all
in one process anyway

thanks very much for your help

Graeme
Am i getting this or am i way off :-)
"Graeme Matthew" <gs*******@ozemail.com.au> wrote in message
news:ND*****************@nnrp1.ozemail.com.au...
Aahz

Thanks, ive actually been using your OSCON slides which have helped a lot.
So it technically correct that all worker threads in a thread pool are
either doing some work or polling the queue to find out if there is a job
to process ?

eg: (pseudocodish :-))

def run(self):

while 1:

self.lock.acquire()

if QueueManager.HasJob:
job = QueueManager.GetFreeJob()
__processJob(job)

self.lock.release()

def __processJob(self):

blah blah blah .....
many thanks ......
"Aahz" <aa**@pythoncraft.com> wrote in message
news:bf**********@panix1.panix.com...
In article <0t****************@nnrp1.ozemail.com.au>,
Graeme Matthew <gs*******@ozemail.com.au> wrote:

I just cannot seem to find any documentation that shows an example of
using the factory method Event() in threads. I have a thread pool and
if there are no jobs in a Queue I want them to wait for something to
be inserted. When a job is inserted I want to send an Event, the first
thread that picks it up runs with the job the rest wait for another
insert Event.


Given that you're already using a Queue, there is no, repeat NO, reason
for using an Event. Just have your threads block on the Queue.
--
Aahz (aa**@pythoncraft.com) <*>

http://www.pythoncraft.com/

A: No.
Q: Is top-posting okay?


Jul 18 '05 #6
On Fri, 2003-07-18 at 18:29, Graeme Matthew wrote:
ok still a bit confused sorry .... first attempt at a thread pool

Am I correct in saying that each thread runs in a continious loop
Yes.
each thread calls queue.get. The get method will block and not return
anything until an item is placed into it. When an item is placed into
it, one of the threads will get assinged a job i.e the first one that
happens to be in use during the cycle ?
I think you have the idea, but I'm not sure (your terminology escapes me
a bit toward the end). What happens is each thread blocks waiting for
something to be placed on the queue. When something is put on the
queue, one of the available threads (i.e. one that is blocking on
queue.get() rather than processing a job). Only a single thread will be
woken for each item that is put on the queue. It isn't knowable which
thread that will be.
The job is returned to the thread, it runs with the job and does all
the processing then returns and calls queue.get again and waits for a
job to become available ?
Yes.
When placing a job via Queue.put() one must acquire a lock place it
and then release it
No. The locking semantics are internal to the Queue object. Do not
concern yourself with it. It simply works. Do not attempt to put locks
around it.
Am i aslo correct in saying that queue is actually doing the blocking
and controls which thread gets the job ?
In a sense (that is, for practical purposes). How it is done internally
by the interpreter isn't important.
Lastly, sorry for all the questions, surely the CPU usage is the same
when the queue is waiting for jobs and when the threads are polling as
theyre all in one process anyway
The threads are asleep while waiting. They don't consume any CPU. The
queue doesn't "wait" for jobs (that is, it doesn't loop, poll or
otherwise consume any CPU time), when you call queue.put() it is a
method call on an object, not a thread.
Am i getting this or am i way off :-)


Just a little way off ;)

Regards,

Cliff

--
My only regret is that I ever was born
-Swans
Jul 18 '05 #7
Graeme Matthew wrote:

ok so code like this is perfectly safe

def run(self):

while 1:

job = queue.get()

__processjob()


It's almost like you aren't even seeing Aahz' replies. ;-)

The above is certainly safe, but can't be terminated easily.
Just use the loop Aahz showed, which is the above plus the
ability to terminate.

-Peter
Jul 18 '05 #8
Graeme Matthew wrote:
here is my code, please criticise it to death i wont take offence
[snip]
#ON A MULTIPLE CPU MACHINE 2 SEPERATE SERVERS SHOULD BE RUN
#THE INCOMING REQUEST WILL NEED TO BE SWITCHED BETWEEN SERVERS
#THIS IS DUE TO THE GLOBAL INTERPRETER LOCK (GIL) IN PYTHON


If my understanding of your intentions is correct, then I think you're
expecting that multiple python threads within the same interpreter
will migrate to multiple processors, and thus execute in parallel,
i.e. simultaneously on multiple processors.

However, the GIL prevents precisely that: if you have one python
thread running on one processor, it will lock the GIL, thus causing
all other python threads on other processors to be suspended until the
GIL is free again.

One way to achieve true concurrency across multiple processors is
to release the GIL when you are processing, which you cannot do from
within python code. You have to write a C language extension to do the
work, that releases the GIL before doing its job, and reacquires it
again when finished.

If you really want true concurrency, and still want to write in pure
python, then one way to do it is run multiple python interpreters in
separate OS processes, which are bound to different processors. Which
means that

1. You can no longer use Queue.Queue objects to communicate between
servers, since your servers no longer share an address space.

2. You have to use some alternate comms mechanism between the two
servers, such as shared memory, named pipes or something like Pyro.

3. You can no longer pass the socket object across wrapped in a "job"
object, since the file descriptor tables of the two server processes
will be different.

Lastly, I have a question of my own: I've read statements along the
lines of "having the GIL in python provides certain guarantees that
make certain things possible". I can't remember specifics. I'd be most
grateful if someone could point me to reading material (apart from the
source, Luke :-) which describes what these guarantees are, and what
they make possible.

TIA,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #9

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

Similar topics

8
8363
by: Ivan Nestlerode | last post by:
Hello comp.lang.python, I am attempting to write a threaded program right now in Python (version 2.3 on Debian unstable) and the current behavior of KeyboardInterrupt is causing me grief. From...
1
1693
by: Simon Roses Femerling | last post by:
Hi all :) Anyone got a nice & simple threading pool example? I'm working on a wxpython app that runs plugins, so I want the plugins to run in a threading pool so the GUI is not blocked. ...
3
539
by: Suhail Salman | last post by:
Dear All, i got the following error message in an applications that opens 13 threads and all of them calling a stored procedure which retreieves data from SQLServer and fills them to a table the...
6
2836
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
0
976
by: BenLeino | last post by:
Hi out there, I have a little problem with threading an event receiving. I have a custom Class (DLL) that raises Events. When I run the Instance without threading it works fine. When I do...
2
1407
by: hecklar | last post by:
This is my first time posting here, so i apologize if i'm posting in the wrong subgroup or whatever, but here goes... I’m having a problem with threading and events (permissions?) in a VB.net...
10
4543
by: jt | last post by:
The program works like this: There is a form with a button. When the form is loaded, a separate thread is started which is retreiving/updating data in the database every x seconds. When clicked...
0
1651
by: roni schuetz | last post by:
since a few day's i'm running around the problem that I stocked with a change i need to do. hopefully somebody here can give me a tipp which will be usefull to solve my problem. I'm using a...
9
2843
by: cmwb2000 | last post by:
I am trying to build a peice of software to survey how a user interacts with a pc. I need to record keystroke latencies and durations etc. I have got the event handler working and I was wanting to...
0
7199
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
7274
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,...
1
6984
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...
0
7453
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...
0
4670
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...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.