【Python】多进程-4

#练习:用event事件控制进程执行顺序,下面例子中,主进程main函数在创建了子进程之后,依然会往下执行,所以会出现主进程先打印出来的情况
import multiprocessing
import time

def wait_for_event(e):
    #Wait for the event to be set before doing anything
    print 'wait_for_event: starting'
    e.wait() # 等待收到能执行信号,如果一直未收到将一直阻塞
    print 'wait_for_event: e.is_set()->', e.is_set()

def wait_for_event_timeout(e, t):
    #Wait t seconds and then timeout
    print 'wait_for_event_timeout: starting'
    e.wait(t)# 等待t秒超时,此时Event的状态仍未未设置,继续执行
    print 'wait_for_event_timeout: e.is_set()->', e.is_set()
    e.set()# 初始内部标志为真

if __name__ == '__main__':
    e = multiprocessing.Event()
    print "begin,e.is_set()", e.is_set()
    w1 = multiprocessing.Process(name='block', target=wait_for_event, args=(e,))
    w1.start()
    
    #可将2改为5,看看执行结果
    w2 = multiprocessing.Process(name='nonblock', target=wait_for_event_timeout, args=(e, 2)) 
    w2.start()

    #e.set()   #可注释此句话看效果

    print 'main: waiting before calling Event.set()'
    time.sleep(3)
    # e.set()   #可注释此句话看效果
    print 'main: event is set'



#练习:管道练习,双工,单工,将受到的消息保存到文件中
import multiprocessing as mp
from multiprocessing import Process,Lock

def write_file(content,lock,file_path="e:\test40.txt"):
    lock.acquire()
    with open(file_path,"a") as f1:
        f1.write(content+"
")
    lock.release()

def proc_1(pipe,lock):
    pipe.send('hello')
    write_file("hello",lock)
    print 'proc_1 received: %s' %pipe.recv()
    pipe.send("what is your name?")
    write_file("what is your name?",lock)
    print 'proc_1 received: %s' %pipe.recv()

def proc_2(pipe,lock):
    print 'proc_2 received: %s' %pipe.recv()
    pipe.send('hello, too')
    write_file('hello, too',lock)
    print 'proc_2 received: %s' %pipe.recv()
    pipe.send("I don't tell you!")
    write_file("I don't tell you!",lock)

if __name__ == '__main__':
    # 创建一个管道对象pipe
    lock=Lock()
    pipe = mp.Pipe()
    print len(pipe)
    print type(pipe)
    # 将第一个pipe对象传给进程1
    p1 = mp.Process(target = proc_1, args = (pipe[0],lock))
    # 将第二个pipe对象传给进程2
    p2 = mp.Process(target = proc_2, args = (pipe[1],lock))
    p2.start()  #这里按理说应该是收的先启起来,但这个例子里p1和p2哪个先启起来没关系
    p1.start()
    p2.join()
    p1.join()



#练习:condition,notify_all通知所有,这个例子里,有可能出现消费者收到消息较快,比生产者消息先打印出来的情况,如果使用notify(),就需要有几个进程就写几个notify()
import multiprocessing as mp
import threading
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()
原文地址:https://www.cnblogs.com/jingsheng99/p/8749055.html