消费者/生产者不等待活动

时间:2015-02-03 20:14:01

标签: python multithreading semaphore python-2.x

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

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

1 个答案:

答案 0 :(得分:4)

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

  1. 这是一种化妆品。使用with mutex:代替所有mutex.acquire()mutex.release(),因为这些内容会自动发生,从而缩短代码并减少错误。
  2. 您应该在获取mutex之前等待您的活动。否则线程将获取互斥锁,然后开始等待其条件变量,但是,应该设置它的步骤不能获得mutex,所以一切都停止了。请注意,当有多个儿子或父母时,必须在锁定mutex后重新检查该事件。这是因为在等待事件之后,可能会在获取mutex之前清除事件。
  3. 等待活动后,您应该对活动采取行动,然后清除它。否则,当子线程设置事件时,父级唤醒并处理它。但是,由于事件仍然设置,如果父母再次醒来,它会再次进行,给出双父线(和双子节)。
  4. 进行这些调整会给我这段代码:

    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"
    

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