threads or queue for this task

robin at execulink.com robin at execulink.com
Sat Sep 14 21:57:06 EDT 2002


Mark Hammond <mhammond at skippinet.com.au> wrote:

>threads = []
>for i in range(number_of_tasks):
>   thread.append(thread_for_task)
># let the threads run - just wait for shutdown
>for t in threads:
>   t.join()
>
>Each thread itself contains a loop which continually look for more work 
>to do, or exit.

Er, quite. For some reason I had not thought of simply having the
worker threads look for work. I was thinking more in terms of a Queue,
where the workers are fed work. But this is likely not even needed. 

In my case I would have them loop indefinitely. Something simple like:

nap = 1
while 1:
    try:
        LookForWork()
		time.sleep(nap)
	except MyInterrupt:
		break
	except:
		raise

Having individual threads sleeping will not adversely effect
performance I imagine. Or is there some reason to avoid mixing the
mechanisms?

>Look for Aahz's threading tutorial - URL not at hand, but it can't be 
>too hard to find.  Indeed, I predict he will tell you exactly where it is ;)

Yes he has! But this slideshow is a little sketchy. Has someone done a
more thorough tutorial covering all the different mechanisms
(semaphore, events, etc.)? 'Twould be useful for a newbie like me.

-- robin



More information about the Python-list mailing list