消费者/生产者甚至不等待

2024-03-29 14:02:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个程序生产者/消费者,在这个程序中我有一个父和一个子,父填充一个共享变量与一些鱼和它发送通知在儿子。 儿子开始吃,如果没有鱼,就通知父母。 我试过这个代码,但不起作用:

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

Tags: andreleasedefparentprintthreadingmutexset
1条回答
网友
1楼 · 发布于 2024-03-29 14:02:36

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

  1. 这个有点像化妆品。使用with mutex:而不是所有的mutex.acquire()mutex.release(),因为这样会自动执行,使代码更短,更不容易出错。你知道吗
  2. 您应该在获取mutex之前等待事件。否则线程将获取互斥锁,然后开始等待其条件变量,然而,应该设置它的线程无法获取mutex,所以一切都停止了。注意,当有多个儿子或父母时,必须在锁定mutex后重新检查事件。这是因为在等待事件之后,可能会在获取mutex之前清除事件。你知道吗
  3. 等待事件发生后,您应该对事件采取行动,然后清除它。否则,当子线程设置事件时,父线程会唤醒并处理它。但是,由于事件仍处于设置状态,如果父级再次唤醒,它将再次进行,并给出双父级行(和双子级部分)。你知道吗

进行这些调整会产生以下代码:

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"

我还没有设法打破这一点(适用于多个儿子或父母),所以我认为这是正确的,但在这一点上的任何更正都是最受欢迎的(因为这是多线程的,奇怪的事情隐藏在并行的阴影中)。你知道吗

相关问题 更多 >