0

I got some problem with two threads which do seem to deadlock

The idea is this:

p1 = threading.Thread(targest  =myClass.setData, args = mylist)
p1.start()
p2 = threading.Thread(target = myClass.takeData, args = mylist)
p2.start()

mylist is a list.

Everything works absolutely fine when either the list is almost empty or when only either p1 or p2 is running. If both run, they seem to deadlock. I have tried to lock them - to no avail.

setData has an infinite while-loop which resets data in mylist, whereas takedata has an infinite whileloop which reads data from mylist.

Is it possible to do what I try to do?

4

1 回答 1

1

正如Adrián López 所说,这是一个生产者-消费者问题。您必须使用信号量来锁定 和 使用的全局 myClass.setData数据myClass.takeData。在这里,您有一个功能示例来获取想法。

http://smherwig.blogspot.com.es/2012/09/producer-consumer-model-with-python.html

于 2013-02-12T12:48:49.520 回答