Threading help?

Cliff Wells logiplexsoftware at earthlink.net
Wed Mar 6 16:26:12 EST 2002


On Wed, 06 Mar 2002 21:39:19 +0100
Ype Kingma wrote:

> VanL wrote:
> > I need three threads (at least):
> > 
> > 1. Every .1 second, retrieves some data via http from an adjacent
> > machine on the network.  Easy enough with urllib.
> > 2. Do processing on that data
> > 3. Every .1 second, be ready to spit out the most recent results from
> > the processing in response to an http query.  I've got the latest
medusa
> > distribution, and I was thinking about using that.   It doesn't really
> > matter, though.

> What would you gain from using separate threads for task 1 and 2, ie.
> what's the point of getting new data (task 1) before the processing on
that
> data (task 2) has finished?

One good reason to use threads in this context is to ensure that the
network port gets serviced in a timely fashion.  How long the processing is
expected to take is the deciding factor in whether threads are appropriate.
 In this case, the OP wants to service the port every .1s.  What if the
processing takes longer than .1s?

> Assuming you combine task 1 and 2 in a single thread B, that thread
should
> yield a few times during your 0.1 second period to allow an evt.
execution
> of task 3 in thread C. In case you have thread priorities available 
> you can give C a higher priority instead of yielding in B.
> 
> In case you decide to have task 1 in a separate thread A you 
> can treat A as C. You might need to yield more often in B with two
> other threads instead of one.
> 
> A simple reference to the last available processing result
> can be shared between any pair of threads. Make sure to 
> copy the reference (not the last result itself) in the receiver.

The approach I would take, based upon the information given, is to have
thread A retrieve the data every .1s (using time.sleep),  when the data is
retrieved, put it on a Queue and go back to sleep.  Thread B blocks on the
Queue until data is available.  When data becomes available, B processes
that data and place it on a second Queue for thread C (thread C handling
HTTP requests) to deal with.

Whether this will occur within the time constraints is difficult to say. 
However, if thread C is simply serving HTTP requests, I imagine it could
simply resend the last processed data if no new data is available when the
request comes in.

Regards,

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308




More information about the Python-list mailing list