python 多进程之间共享变量

多线程 进程间共享变量等

1、进程间共享变量 self.val=Value('i',init_value)

复制代码

#进程间共享变量及列表类型
class Counter(object):
    def __init__(self, ini=0):
        self.val = Value('i', ini)
        self.lock = Lock()

    def increment(self):
        with self.lock:
            self.val.value=self.val.value+1
        print('共享变量val自增1次:', self.value())

    def value(self):
        with self.lock:  #这个with self.lock:  注释掉之后结果是一样的

            return self.val.value


def func(counter_1):
    for i in range(5):
        time.sleep(0.01)
        counter_1.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())

复制代码

2、通过 Value、Array实现进程间变量共享

复制代码

from multiprocessing import Array,Value

#进程间共享变量及列表类型
def f(n,a):
        n.value+=100
        for i in range(len(a)):
            a[i]*=-1

if __name__=='__main__':
        num=Value('i',0)        #理解成是一个共享对象
        print('type(num):',type(num))
        arr=Array('i',range(10))    #理解成是一个共享对象
        print('type(arr):', type(arr))
        p=Process(target=f,args=(num,arr))
        p.start()
        p.join()
        print('num:',num.value)
        print('arr:',list(arr))
        print('主程序执行完成!!')

复制代码

3、进程间共享字符串from ctypes import c_char_p  s=Value(c_char_p,'hello')

复制代码

from multiprocessing import Process,Manager,Value
from ctypes import c_char_p
def greet(sharestr):
        sharestr.value+=',world'

if __name__=='__main__':
        manager=Manager()
        print(type(manager))
        sharestr=manager.Value(c_char_p,'hello')
        p=Process(target=greet,args=(sharestr,))
        p.start()
        p.join()
        print(sharestr.value)
        print(type(sharestr))

复制代码

4、进程间共享列表、字典 manager=Manager()   manager.dict()  manager.list(range(10))

复制代码

def func(shared,sharel):
        shared[1]='1'
        shared['2']='2'
        shared[0.25]=None
        sharel.reverse()
if __name__=='__main__':
        manager=Manager()
        shared=manager.dict()
        sharel=manager.list(range(10))
        p=Process(target=func,args=(shared,sharel))
        p.start()
        p.join()
        print(shared)
        print(sharel)

复制代码

5、进程间共享实例对象 BaseManager()

复制代码

import time,os
import random
from multiprocessing import  Pool,Value,Lock
from multiprocessing.managers import BaseManager
class MyManager(BaseManager):
        pass

def Manager():
        m=MyManager()
        m.start()
        return m
class Counter():
        def __init__(self,ini=0):
            self.val=Value('i',ini)
            self.lock=Lock()
        def increment(self):
            with self.lock:
                    self.val.value=self.val.value+1
        def value11(self):
                with self.lock:
                        return self.val.value
MyManager.register('Counter',Counter)

def long_time_task(name,counter):
        time.sleep(0.2)
        print(' run task %s(%s)\n'%(name,os.getpid()))

        start=time.time()
        for i in range(10):
                time.sleep(0.2)
                counter.increment()
        end=time.time()
        print('task %s runs %s seconds.'%(name,(end-start)))

if __name__=='__main__':
        manager=Manager()
        counter=manager.Counter(0)
        print('Parent process %s.'%os.getpid())
        #p=Pool()
        ps=[]
        for i in range(5):
            ps.append(Process(target=long_time_task,args=(str(i),counter)))
            #p.apply_async(long_time_task,args=(str(i),counter))
            #p.apply_async(target=long_time_task,args=(str(i),counter))
        print('等待所有进程完成:')
        #p.close()
        for i in ps:
                i.start()
        for i in ps:
            i.join()
        #p.join()
        print('全部完成')
        print('counter.value11():',counter.value11())

复制代码

方法二:

复制代码

import time,os
import random
import multiprocessing as multip
from multiprocessing import  Pool,Value,Lock,Process
from multiprocessing.managers import BaseManager
class MyManager(BaseManager):
        pass

def Manager():
        m=MyManager()
        m.start()
        return m

class Counter():
        def __init__(self,ini=0):
            self.val=Value('i',ini)
            self.lock=Lock()
        def increment(self):
            with self.lock:
                    self.val.value=self.val.value+1
        def value11(self):
                with self.lock:
                        return self.val.value
MyManager.register('Counter',Counter)

def long_time_task(name,counter):
        time.sleep(0.2)
        print(' run task %s(%s)\n'%(name,os.getpid()))

        start=time.time()
        for i in range(10):
                time.sleep(0.2)
                counter.increment()
        end=time.time()
        print('task %s runs %s seconds.'%(name,(end-start)))

if __name__=='__main__':
        #MyManager.register('Counter', Counter)
        manager=Manager()

        counter=manager.Counter(0)
        print('Parent process %s.'%os.getpid())

        p=Pool()
        for i in range(5):
            p.apply_async(long_time_task,args=(str(i),counter))

        #p.apply_async(long_time_task,args=(str(i),counter))
        #p.apply_async(target=long_time_task,args=(str(i),counter))
        print('等待所有进程完成:')
        p.close()

        p.join()
        print('全部完成')
        print('counter.value11():',counter.value11())

复制代码

方法三:

复制代码

import multiprocessing as mp,time,random
from multiprocessing import *
from multiprocessing.managers import BaseManager


class Counter():
        def __init__(self,a):
            self.val=Value('i',a)
            self.lock=Lock()
        def increment(self):
            with self.lock:
                self.val.value+=1
            #print('self.val的值为:%s'%self.val.value)
        def get_value(self):
            with self.lock:
                return self.val.value

class MyManager(BaseManager):
    pass

MyManager.register('Counter_copy',Counter)

def func(counter,a):
        time.sleep(0.1)
        for i in range(10):
            counter.increment()
        print('%s次的值为:%s'%(a,counter.get_value()))

if __name__=='__main__':
        m=MyManager()
        m.start()
        counter=m.Counter_copy(0)

        pool=Pool(2)
        for i in range(5):
                pool.apply_async(func,args=(counter,str(i)))
        pool.close()
        pool.join()

        print('主程序执行完成!!')

你可能感兴趣的:(Python高级)