多进程编程(二)

进程间协同

进程间写操作需要加锁,需要处理进程间协同的问题。

进程间协同的方式:Queue、Lock、Semaphore、Event、Pipe、Condition。

1、Queue -->put()

                       get()

                       empty()

                       JoinableQueue-->task.done()

                                                      join()

----------------------------------------------------------------------------------------------------------------

2、Lock -->acquire()#申请锁

                     release()#释放锁

---------------------------------------------------------------------------------------------------

练习1:按顺序打印

# 没有加锁
def func(num):
    time.sleep(0.1)
    print('hello Num:%s'%(num))

if __name__ == '__main__':
    for num in range(50):
        Process(target=func,args=(num,)).start()

# 执行结果

hello Num:6
hello Num:21
hello Num:42
hello Num:17
hello Num:2
hello Num:10
hello Num:3
hello Num:7
hello Num:14
hello Num:18
 

#加锁后按顺序打印

from multiprocessing import Process,Value,Lock
import time

def func(num,lock):
    lock.acquire()
    time.sleep(0.1)
    print('hello Num:%s'%(num))#按顺序打印
    num.value +=1
    lock.release()

if __name__ =='__main__':
    lock=Lock()
    val = Value('i',0)
    procs =[Process(target=func,args=(val,lock)) for i in range(50)]
    for p in procs:
        p.start()
    for p in procs:
        p.join()

    print(val.value)

#执行结果

hello Num:<Synchronized wrapper for c_long(42)>
hello Num:<Synchronized wrapper for c_long(43)>
hello Num:<Synchronized wrapper for c_long(44)>
hello Num:<Synchronized wrapper for c_long(45)>
hello Num:<Synchronized wrapper for c_long(46)>
hello Num:<Synchronized wrapper for c_long(47)>
hello Num:<Synchronized wrapper for c_long(48)>
hello Num:<Synchronized wrapper for c_long(49)>
50

# 练习:修改Count类,使用lock,保证写加了锁
from multiprocessing import Process,Value,Lock
import time
class Count(object):
    def __init__(self,count):
        self.count = Value('d',count)

    def increment(self,lock):
        lock.acquire()
        self.count.value +=1
        lock.release()

    def getValue(self):
        return self.count.value

if __name__ == '__main__':
    c1=Count(5)
    lock =Lock()
    processList = []
    for i in range(50):
        processList.append(Process(target=c1.increment,args=(lock,)))
    for i in range(50):
        processList[i].start()
    for i in range(50):
        processList[i].join()

    print(c1.getValue())
# 修改代码使其按顺序打印
from multiprocessing import Process,Value,Lock
import time

def l(lock,num):
    lock.acquire()
    time.sleep(0.1)
    print('hello Num:%s'%(num))#按顺序打印
    lock.release()

if __name__ =='__main__':
    lock=Lock()
    for num in range(20):
        Process(target= l,args=(lock,num)).start()

#执行结果

hello Num:17
hello Num:13
hello Num:19
hello Num:9
hello Num:10
hello Num:6
hello Num:8
hello Num:18
hello Num:11
hello Num:14

#修改后

def l(lock,num):
    lock.acquire()
    # time.sleep(0.1)
    print('hello Num:%s'%(num))#按顺序打印
    lock.release()

if __name__ =='__main__':
    lock=Lock()
    for num in range(20):
        Process(target= l,args=(lock,num)).start()
        time.sleep(0.1)

3、Semaphore -->acquire()

                                release()

--------------------------------------------------------------------------

4、Event  --> is_set()

                        set()

                         wait([time])

                          clear()

-----------------------------------------------------------------------------------------------

 

5、Pipe -->pipe = mp.Pipe()

                    pipe[0].send()~pipe[1].recv()

                    pipe[1].send()~ pipe[0].recv()

----------------------------------------------------------------------------

6、Condition

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值