测试代码:
import time
import multiprocessing
import threading
class A(object):
def __init__(self):
self.data = 1
th = threading.Thread(target=self.start)
th.start()
def start(self):
print('*****************')
while True:
self.data += 1
time.sleep(1)
class server(object):
def __init__(self):
self.a = A()
def main_thread(self):
print('---------------')
while True:
print(self.a.data)
time.sleep(3)
def run(self):
process = multiprocessing.Process(target=self.main_thread)
process.start()
process.join()
if __name__ == '__main__':
s = server()
s.run()
没有公布答案前你猜猜会print多少?你会发现一直打印2出来,原因是多进程变量不能这么玩!正确方式为:
import time
import multiprocessing
import threading
class A(object):
def __init__(self):
self.data = multiprocessing.Manager().dict()
self.data['num'] = 1
th = threading.Thread(target=self.start)
th.start()
def start(self):
print('*****************')
while True:
self.data['num'] += 1
time.sleep(1)
class server(object):
def __init__(self):
self.a = A()
def main_thread(self):
print('---------------')
while True:
print(self.a.data['num'])
time.sleep(3)
def run(self):
process = multiprocessing.Process(target=self.main_thread)
process.start()
process.join()
if __name__ == '__main__':
s = server()
s.run()
要从变量定义着手。具体可以参考博客Python3多进程共享变量实现方法 - 诸子流 - 博客园
今天同事反映一个问题让帮忙看一下:多进程共用一个变量,在一个进程中修改后,在另外的进程中并没有产生修改。
最初以为是没添加global声明导致修改未生效,但实际操作发现global方式在多进程中也只能读不能写。错误示例代码如下:
import multiprocessing # 声明一个全局变量 share_var = ["start flag"] def sub_process(process_name): # 企图像单个进程那样通过global声明使用全局变量 global share_var share_var.append(process_name) # 但是很可惜,在多进程中这样引用只能读,修改其他进程不会同步改变 for item in share_var: print(f"{process_name}-{item}") pass def main_process(): process_list = [] # 创建进程1 process_name = "process 1" tmp_process = multiprocessing.Process(target=sub_process,args=(process_name,)) process_list.append(tmp_process) # 创建进程2 process_name = "process 2" tmp_process = multiprocessing.Process(target=sub_process, args=(process_name,)) process_list.append(tmp_process) # 启动所有进程 for process in process_list: process.start() for process in process_list: process.join() if __name__ == "__main__": main_process()
执行结果如下,可以看到进程1中的修改未表现在进程2中(不过要注意,和多线程一样,如果运算量再大一点进程1并不一定比进程2先执行):
参考:Python 进程之间共享数据(全局变量)_houyanhua1的博客-CSDN博客_python process 共享变量
import multiprocessing # 不能将共享变量和共享锁定义成全局变量然后通过global引用那样会报错,只能传过来 def sub_process(process_name,share_var,share_lock): # 获取锁 share_lock.acquire() share_var.append(process_name) # 释放锁 share_lock.release() for item in share_var: print(f"{process_name}-{item}") pass def main_process(): # 单个值声明方式。typecode是进制类型,C写法和Python写法都可以,见下方表格;value是初始值。 # 这种单值形式取值赋值需要通过get()/set()方法进行,不能直接如一般变量那样取值赋值 # share_var = multiprocessing.Manager().Value(typecode, value) # 数组声明方式。typecode是数组变量中的变量类型,sequence是数组初始值 # share_var = multiprocessing.Manager().Array(typecode, sequence) # 字典声明方式 # share_var = multiprocessing.Manager().dict() # 列表声明方式 share_var = multiprocessing.Manager().list() share_var.append("start flag") # 声明一个进程级共享锁 # 不要给多进程传threading.Lock()或者queue.Queue()等使用线程锁的变量,得用其进程级相对应的类 # 不然会报如“TypeError: can't pickle _thread.lock objects”之类的报错 share_lock = multiprocessing.Manager().Lock() process_list = [] process_name = "process 1" tmp_process = multiprocessing.Process(target=sub_process,args=(process_name,share_var,share_lock)) process_list.append(tmp_process) process_name = "process 2" tmp_process = multiprocessing.Process(target=sub_process, args=(process_name,share_var,share_lock)) process_list.append(tmp_process) for process in process_list: process.start() for process in process_list: process.join() if __name__ == "__main__": main_process()
执行结果如下,可以看到进程1中的修改已表现在进程2中(不过要注意,和多线程一样,如果运算量再大一点进程1并不一定比进程2先执行):
typecode如果是数值或单个字符,可为以下类型(注意有引号):
Type Code | C Type | Python Type |
'c' | char | character |
'b' | signed char | int |
'B' | unsigned char | int |
'u' | Py_UNICODE | unicode character |
'h' | signed short | int |
'H' | unsigned short | int |
'i' | signed int | int |
'I' | unsigned int | int |
'l' | signed long | int |
'L' | unsigned long | int |
'f' | float | float |
'd' | double | float |
如果是字符串类型,typecode可为以下第一列形式(注意无引号):
ctypes type | C type | Python type |
c_bool |
_Bool | bool (1) |
char | char | 1-character string |
c_wchar | wchar_t | 1-character unicode string |
c_byte | char | int/long |
c_ubyte | unsigned char | int/long |
c_short | short | int/long |
c_ushort | unsigned short | int/long |
c_int | int | int/long |
c_uint | unsigned in | int/long |
c_long | long | int/long |
c_ulong | unsigned long | int/long |
c_longlong | __int64 or long long | int/long |
c_ulonglong | unsigned __int64 or unsigned long long | int/long |
c_float | float | float |
c_double | double | float |
c_longdouble | long double | float |
c_char_p | char * (NUL terminated) | string or None |
c_wchar_p |
wchar_t * (NUL terminated) | unicode or None |
c_void_p | void * | int/long or None |
同事还想共享一个文件对象,然后问上边的方法是不是只能共享字典、列表,没法共享对象。
回头一看,Value和Array中typecode要求是c语言中存在的类型,其他只有dict()和list()方法没有其他方法,所以似乎上边的方法共享实例化对象是不行的。
但我们前面说过global方式不可以修改,但读还是没问题的;所以对象引用还是可以使用global方式。
import multiprocessing import threading # 实例化一个全局文件对象 file_obj = open("1.txt","a") share_lock = threading.Lock() def sub_process(process_name): global file_obj,share_lock share_lock.acquire() file_obj.writelines(f"{process_name}") share_lock.release() pass def main_process(): process_list = [] # 创建进程1 process_name = "process 1" tmp_process = multiprocessing.Process(target=sub_process,args=(process_name,)) process_list.append(tmp_process) # 创建进程2 process_name = "process 2" tmp_process = multiprocessing.Process(target=sub_process, args=(process_name,)) process_list.append(tmp_process) # 启动所有进程 for process in process_list: process.start() for process in process_list: process.join() if __name__ == "__main__": main_process()
global方式不能修改变量(如要修改其成员变量),在大多时候也是可以了,但总让人觉得不是一种完美的实现方法。有没有可以修改的实现方法呢,答案是有的,可以使用BaseManager。示例代码如下。
参考:python 多进程共享类实例demo_AI视觉网奇的博客-CSDN博客_python 多进程共享类实例demo
import multiprocessing from multiprocessing.managers import BaseManager import threading # 锁可以通过global也可以在Process中传无所谓 share_lock = threading.Lock() # 定义一个要共享实例化对象的类 class Test(): def __init__(self): self.test_list = ["start flag"] def test_function(self,arg): self.test_list.append(arg) def print_test_list(self): for item in self.test_list: print(f"{item}") def sub_process(process_name,obj): global share_lock share_lock.acquire() obj.test_function(f"{process_name}") share_lock.release() obj.print_test_list() pass def main_process(): # 如果是想注册open方法这样操作 # manager = BaseManager() # # 一定要在start前注册,不然就注册无效 # manager.register('open', open) # manager.start() # obj = manager.open("1.txt","a") # 为了更加直接我们直接以一个Test类的实例化对象来演示 manager = BaseManager() # 一定要在start前注册,不然就注册无效 manager.register('Test', Test) manager.start() obj = manager.Test() process_list = [] # 创建进程1 process_name = "process 1" tmp_process = multiprocessing.Process(target=sub_process,args=(process_name,obj)) process_list.append(tmp_process) # 创建进程2 process_name = "process 2" tmp_process = multiprocessing.Process(target=sub_process, args=(process_name,obj)) process_list.append(tmp_process) # 启动所有进程 for process in process_list: process.start() for process in process_list: process.join() if __name__ == "__main__": main_process()
执行结果如下,可以看到进程1中的修改已表现在进程2中(不过要注意,和多线程一样,如果运算量再大一点进程1并不一定比进程2先执行):
参考:
Python:线程、进程与协程(5)——multiprocessing模块(2)_11016142的技术博客_51CTO博客
multiprocessing — Process-based parallelism — Python 3.11.0 documentation