多线程 multiprocessing 的几个小例子

1、Pipe

import multiprocessing as multip,time
from multiprocessing import Process,Pipe,Event,Condition,Lock,Pool,Value,Array

def pipe1(pipe):
    pipe.send('hello')
    print('p1.recv1',pipe.recv())
    pipe.send('what is your name??')
    print('p1.recv2',pipe.recv())

def pipe2(pipe):
    print('p2.recv1',pipe.recv())
    pipe.send('hello,too')
    print('p2.recv2',pipe.recv())
    pipe.send('i do not tell you!!')

if __name__=='__main__':
    pipe=Pipe()

    p1=Process(target=pipe1,args=(pipe[0],))
    p2=Process(target=pipe2,args=(pipe[1],))

    p1.start()
    p2.start()
    #p1.join()
    #p2.join()

    print('主程序结束运行')

2、Event

def event1(e):
    print('event1 开始运行:')
    e.wait()
    print('event1 等待结束:')

def event2(e,t):
    print('event2 开始运行:')
    e.wait(t)
    print('event2 等待超时结束:')
    e.set()

if __name__=='__main__':

    e=Event()

    p1=Process(target=event1,args=(e,))
    p2=Process(target=event2,args=(e,4))
    
    p1.start()
    p2.start()

    time.sleep(3)
    print('主程序运行结束!')

3、Condition

def cond1(cond):
    with cond:
        print('cond1 开始运行',multip.current_process().name)
        cond.wait()
        print('cond1 等待结束',multip.current_process().name)

def cond2_notify(cond):
    with cond:
        print('cond2 开始运行',multip.current_process().name)
        cond.notify_all()#notify() 只通知一个等待的condition
        print('cond2 notifyall结束',multip.current_process().name)

if __name__=='__main__':

    con=Condition()

    p1=Process(target=cond1,args=(con,))
    p2=Process(target=cond1,args=(con,))
    p3=Process(target=cond2_notify,args=(con,))
    
    p1.start()
    p2.start()
    time.sleep(4)
    p3.start()

    time.sleep(3)
    print('主程序运行结束!')

4、Lock

def func(lock):
#def func():
    lock.acquire()
    print('进程锁之后开始运行:',multip.current_process().name)
    time.sleep(2)
    print('下面释放进程锁:',multip.current_process().name)
    lock.release()

if __name__=='__main__':

    lock=Lock()
    for i in range(10):
        p1=Process(target=func,args=(lock,))
        p1.start()
    
    time.sleep(3)
    print('主程序运行结束!')

5、Pool

def func(n):
    time.sleep(0.2)
    return n**2
if __name__=='__main__':
    pool=Pool(3)
    ll=list(range(10))
    result=pool.map(func,ll)
    print('result:',result)

6、进程间共享变量 (后面补充)

class Counter():
    def __init__(self,ini=0):
        self.val=Value('i',ini)
        self.lock=Lock()
    def increment(self):
        with self.lock:
        
            self.val.value+=1
        print('共享变量val自增1次:',self.value())#在调用value()时,如果2个锁重叠,可能会死锁
        
    def value(self):
        #with self.lock:
        return self.val.value
        
def func(counter):
    for i in range(5):
        time.sleep(0.01)
        counter.increment()

if __name__=='__main__':
    counter=Counter(0)
    ps=[Process(target=func,args=(counter,)) for i in range(5)]
    for i in ps:
        i.start()
    for i in ps:
        i.join()
    print('counter.value():',counter.value())

 

posted @ 2019-02-17 10:27  小金儿  阅读(693)  评论(0编辑  收藏  举报