Python Multithreading Problem: Abnormal Program Termination
Verfasst: Mittwoch 7. Januar 2009, 09:27
I'm having difficulties with multi-threading using Python. For instance, the example script given below either crashes in lines 28 or 29 ("abnormal program termination") or hangs infinitely in line 30, and I do not know why. It simply tries to compute Fibonacci numbers in more than 1 thread (the Fibonacci code itself works and is taken from the Python tutorial).
Can anybody help? Thanks in advance!
Sabine Lorentz
[/quote]
Can anybody help? Thanks in advance!
Sabine Lorentz
Code: Alles auswählen
from Queue import Queue
from threading import Thread
num_worker_threads = 2
def do_work(c):
a, b = 0, 1
while b < c:
#print b
a, b = b, a+b
print b
return b;
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
return;
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.setDaemon(True)
t.start()
print "Start."
#q.put(10)
q.put(301)
q.put(25)
q.join(5) # block until all tasks are do
#o = do_work(25);
#print o
#o = do_work(301);
#print o
print "End."