消费者/生产者不等待事件
Consumer / producer does not wait for event
我想写一个程序producer/consumer,在这个程序中我有一个parent和一个儿子,parent用一些鱼填充一个共享变量并发送通知在儿子。
儿子开始吃,没有鱼就通知parent。
我试过这段代码,但它不起作用:
import threading
import time
NUM_FISH = 13
mutex = threading.Lock()
mutParent = threading.Event()
mutSon = threading.Event()
fish = NUM_FISH
def set(fish1):
global fish
fish = fish1
def get():
return fish
def parent(mutParent, mutSon):
while True:
mutex.acquire()
mutParent.wait()
time.sleep(0.5)
try:
set(NUM_FISH)
print " + parent brings %d fish\n" % fish
mutex.release()
mutSon.set()
except:
print "Exception"
mutex.release()
def son(id, mutParent, mutSon):
while True:
mutex.acquire()
mutSon.wait()
fish = get() - 1
set(fish)
time.sleep(0.5)
try:
if fish > 0 :
print " - Son %d eats (dish: %d fish)\n" % (id, fish)
mutex.release()
else:
print " - Son %d eats (dish: %d fish) and screams\n\n" % (id, fish)
mutex.release()
mutParent.set()
except:
print "Exception"
mutex.release()
print "\n + intitial dish: %d fish\n\n" % fish
mutSon.set()
t2 = threading.Thread(target=son, args=(1, mutParent, mutSon))
t2.start()
t1 = threading.Thread(target=parent, args = (mutParent, mutSon))
t1.start()
t2.join()
t1.join()
这是我的输出:
myself@ubuntu:~/Desktop$ python a.py
+ intitial dish: 13 fish
- Son 1 eats (dish: 12 fish)
- Son 1 eats (dish: 11 fish)
- Son 1 eats (dish: 10 fish)
- Son 1 eats (dish: 9 fish)
- Son 1 eats (dish: 8 fish)
- Son 1 eats (dish: 7 fish)
- Son 1 eats (dish: 6 fish)
- Son 1 eats (dish: 5 fish)
- Son 1 eats (dish: 4 fish)
- Son 1 eats (dish: 3 fish)
- Son 1 eats (dish: 2 fish)
- Son 1 eats (dish: 1 fish)
- Son 1 eats (dish: 0 fish) and screams
- Son 1 eats (dish: -1 fish) and screams
- Son 1 eats (dish: -2 fish) and screams
- Son 1 eats (dish: -3 fish) and screams
- Son 1 eats (dish: -4 fish) and screams
- Son 1 eats (dish: -5 fish) and screams
+ parent brings 13 fish
+ parent brings 13 fish
OK,这里可以改三样东西:
- 这是一种装饰品。使用
with mutex:
而不是所有的 mutex.acquire()
和 mutex.release()
,因为那样事情会自动发生,从而使代码更短且更不容易出错。
- 您应该在获得
mutex
之前等待您的事件。否则一个线程将获得互斥量,然后开始等待它的条件变量,但是,应该设置它的线程无法获得mutex
,所以一切都停止了。注意,当有多个儿子或parent时,事件必须在锁定mutex
后重新检查。这是因为等待事件后,事件可能在mutex
获取到之前被清除
- 等待一个事件后,你应该对事件进行操作,然后清除它。否则,当子线程设置事件时,parent被唤醒并处理它。然而,由于事件仍然设置,如果 parent 再次醒来,它会再次进行,给出双 parent 行(和双子部分)。
进行这些调整后得到以下代码:
def parent(id, mutParent, mutSon):
while True:
mutParent.wait()
with mutex:
if not mutParent.is_set():
continue
time.sleep(0.5)
try:
set(NUM_FISH)
print " + Parent %d brings %d fish\n" % (id, fish)
mutParent.clear()
mutSon.set()
except:
print "Exception"
def son(id, mutParent, mutSon):
while True:
mutSon.wait()
with mutex:
if not mutSon.is_set():
continue
fish = get() - 1
set(fish)
time.sleep(0.5)
try:
if fish > 0:
print " - Son %d eats (dish: %d fish)\n" % (id, fish)
else:
print " - Son %d eats (dish: %d fish) and screams\n\n" % (id, fish)
mutSon.clear()
mutParent.set()
except:
print "Exception"
我没能解决这个问题(与多个儿子或 parents 一起工作),所以我认为它是正确的,但欢迎对这一点进行任何更正(因为这是多线程的,而且很奇怪事物存在于并行的阴影中)。
我想写一个程序producer/consumer,在这个程序中我有一个parent和一个儿子,parent用一些鱼填充一个共享变量并发送通知在儿子。 儿子开始吃,没有鱼就通知parent。 我试过这段代码,但它不起作用:
import threading
import time
NUM_FISH = 13
mutex = threading.Lock()
mutParent = threading.Event()
mutSon = threading.Event()
fish = NUM_FISH
def set(fish1):
global fish
fish = fish1
def get():
return fish
def parent(mutParent, mutSon):
while True:
mutex.acquire()
mutParent.wait()
time.sleep(0.5)
try:
set(NUM_FISH)
print " + parent brings %d fish\n" % fish
mutex.release()
mutSon.set()
except:
print "Exception"
mutex.release()
def son(id, mutParent, mutSon):
while True:
mutex.acquire()
mutSon.wait()
fish = get() - 1
set(fish)
time.sleep(0.5)
try:
if fish > 0 :
print " - Son %d eats (dish: %d fish)\n" % (id, fish)
mutex.release()
else:
print " - Son %d eats (dish: %d fish) and screams\n\n" % (id, fish)
mutex.release()
mutParent.set()
except:
print "Exception"
mutex.release()
print "\n + intitial dish: %d fish\n\n" % fish
mutSon.set()
t2 = threading.Thread(target=son, args=(1, mutParent, mutSon))
t2.start()
t1 = threading.Thread(target=parent, args = (mutParent, mutSon))
t1.start()
t2.join()
t1.join()
这是我的输出:
myself@ubuntu:~/Desktop$ python a.py
+ intitial dish: 13 fish
- Son 1 eats (dish: 12 fish)
- Son 1 eats (dish: 11 fish)
- Son 1 eats (dish: 10 fish)
- Son 1 eats (dish: 9 fish)
- Son 1 eats (dish: 8 fish)
- Son 1 eats (dish: 7 fish)
- Son 1 eats (dish: 6 fish)
- Son 1 eats (dish: 5 fish)
- Son 1 eats (dish: 4 fish)
- Son 1 eats (dish: 3 fish)
- Son 1 eats (dish: 2 fish)
- Son 1 eats (dish: 1 fish)
- Son 1 eats (dish: 0 fish) and screams
- Son 1 eats (dish: -1 fish) and screams
- Son 1 eats (dish: -2 fish) and screams
- Son 1 eats (dish: -3 fish) and screams
- Son 1 eats (dish: -4 fish) and screams
- Son 1 eats (dish: -5 fish) and screams
+ parent brings 13 fish
+ parent brings 13 fish
OK,这里可以改三样东西:
- 这是一种装饰品。使用
with mutex:
而不是所有的mutex.acquire()
和mutex.release()
,因为那样事情会自动发生,从而使代码更短且更不容易出错。 - 您应该在获得
mutex
之前等待您的事件。否则一个线程将获得互斥量,然后开始等待它的条件变量,但是,应该设置它的线程无法获得mutex
,所以一切都停止了。注意,当有多个儿子或parent时,事件必须在锁定mutex
后重新检查。这是因为等待事件后,事件可能在mutex
获取到之前被清除 - 等待一个事件后,你应该对事件进行操作,然后清除它。否则,当子线程设置事件时,parent被唤醒并处理它。然而,由于事件仍然设置,如果 parent 再次醒来,它会再次进行,给出双 parent 行(和双子部分)。
进行这些调整后得到以下代码:
def parent(id, mutParent, mutSon):
while True:
mutParent.wait()
with mutex:
if not mutParent.is_set():
continue
time.sleep(0.5)
try:
set(NUM_FISH)
print " + Parent %d brings %d fish\n" % (id, fish)
mutParent.clear()
mutSon.set()
except:
print "Exception"
def son(id, mutParent, mutSon):
while True:
mutSon.wait()
with mutex:
if not mutSon.is_set():
continue
fish = get() - 1
set(fish)
time.sleep(0.5)
try:
if fish > 0:
print " - Son %d eats (dish: %d fish)\n" % (id, fish)
else:
print " - Son %d eats (dish: %d fish) and screams\n\n" % (id, fish)
mutSon.clear()
mutParent.set()
except:
print "Exception"
我没能解决这个问题(与多个儿子或 parents 一起工作),所以我认为它是正确的,但欢迎对这一点进行任何更正(因为这是多线程的,而且很奇怪事物存在于并行的阴影中)。