Threading help?

Ype Kingma ykingma at accessforall.nl
Wed Mar 6 15:39:19 EST 2002


Van,

VanL wrote:
> 
> Hello,
> 
> This is my first time playing with python threads, and I am a little at
> a loss how to start.  Here is what I need to do:
> 
> 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.
> 
> As you can see, I have two things that have soft real-time deadlines,
> and one thing that I want to do as fast as possible, but doesn't need to
> be real time.  This sounds like a job for threads.
> 
> I can do each of the individual parts, but I don't know how to combine
> them all so that each is running in a different thread, so that process
> 2 doesn't make the other two lose their timing.
> 
> Any help?

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?

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.

Have fun,
Ype

P.S. Yielding is bit ugly in CPython. Try time.sleep(0.001).



More information about the Python-list mailing list