其他分享
首页 > 其他分享> > 消费者/生产者不等待事件

消费者/生产者不等待事件

作者:互联网

我想写一个程序生产者/消费者,在这个程序中我有一个父母和一个儿子,父母用一些鱼填充了一个共享变量,并在儿子处发送通知.
儿子开始进食,如果没有鱼,它会通知父母.
我尝试了这段代码,但是没有用:

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

解决方法:

好的,这里可以更改三件事:

>这是一种化妆品.与互斥锁一起使用:而不是所有的mutex.acquire()和mutex.release(),因为那样的话,这些东西会自动发生,从而使代码更短,更不易出错.
>在获取互斥锁之前,您应该等待事件.否则,线程将获取互斥量,然后开始等待其条件变量,但是,本应设置其的脚踏板无法获取互斥量,因此一切都停止了.请注意,如果有多个儿子或父母,则必须在锁定互斥锁之后重新检查该事件.这是因为在等待事件之后,可以在获取互斥之前清除事件.
>等待事件后,您应该对事件进行操作,然后清除它.否则,当子线程设置事件时,父线程将唤醒并处理该事件.但是,由于该事件仍然设置,因此如果父级再次唤醒,它将再次进行,给出双父级行(和双子节).

进行这些调整后,我得到了以下代码:

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"

我没有设法打破这一点(与多个儿子或父母一起工作),所以我认为它是正确的,但是在这一点上的任何更正都受到欢迎(因为这是多线程的,而且奇怪的事情在于并行性的阴影中).

标签:multithreading,python-2-x,semaphore,python
来源: https://codeday.me/bug/20191028/1954429.html