python进程同步,condition例子

#coding=utf-8
import multiprocessing as mp
import time

def consumer(cond):
    with cond:
        print "consumer before wait"
        cond.wait()
        print "consumer after wait"

def producer(cond):
    with cond:
        print "producer before notifyAll"
        cond.notify_all()
        print "producer after notifyAll"

if __name__ =='__main__':
    condition=mp.Condition()
   
    p1=mp.Process(name='p1',target=consumer,args=(condition,))
    p2=mp.Process(name='p2',target=consumer,args=(condition,))
    p3=mp.Process(name='p3',target=producer,args=(condition,))
   
    p1.start()
    time.sleep(2)
    p2.start()
    time.sleep(2)
    p3.start()
   

c:\Python27\Scripts>python task_test.py
consumer before wait
consumer before wait
producer before notifyAll
producer after notifyAll
consumer after wait
consumer after wait

 

posted @ 2018-04-09 14:00  夏晓旭  阅读(300)  评论(0)    收藏  举报