[Tutor] thread causes idle to crash

Jeff Peery jeffpeery at yahoo.com
Fri Aug 28 17:21:55 CEST 2009


hello,
I've been working on threads and queues and I think I have it working well. The problem seems to be that when I run a thread via the IDLE it crashes. if I simply double click the file it appears to run ok. I'm not sure if I'm doing something incorrectly or not. it seems pretty straight forward. my sample code is listed below. I'm using Python 2.6.2, and I'm running on Microsoft Vista. Any thoughts would be much appreciated. thanks!
Jeff
 
import threading
import Queue
import time, random
WORKERS = 2
 
class Worker(threading.Thread):
    def __init__(self, queue):
        self.__queue = queue
        threading.Thread.__init__(self)
    def run(self):
        while 1:
            item = self.__queue.get()
            if item is None:
                break # reached end of queue
            # pretend we're doing something that takes 10-100 ms
            time.sleep(random.randint(10, 100) / 100.0)
            print "task", item, "finished"
#
# try it
queue = Queue.Queue(0)
for i in range(WORKERS):
    print 'starting'
    Worker(queue).start() # start a worker
for i in range(10):
    print 'putting'
    queue.put(i)
for i in range(WORKERS):
    print 'putting None'
    queue.put(None) # add end-of-queue markers
 


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090828/7ccf3e46/attachment-0001.htm>


More information about the Tutor mailing list