python类内部实现多进程
工作中写一个自动化脚本, 为了提高效率, 需要在类的内部启多进程, 代码逻辑如下:
# –*– coding: utf-8 –*–
# @Time : 2019/3/19 21:11
# @Author : Damon_duanlei
# @FileName : process.py
# @BlogsAddr : https://blog.csdn.net/Damon_duanlei
import time
class A(object):
def __init__(self):
self.a = None
self.b = None
def get_num_a(self):
print("计算逻辑A")
time.sleep(3)
self.a = 10
def get_num_b(self):
print("计算逻辑B")
time.sleep(5)
self.b = 6
def sum(self):
print("a的值为:{}".format(self.a))
print("b的值为:{}".format(self.b))
ret = self.a + self.b
return ret
def run(self):
self.get_num_a()
self.get_num_b()
print(self.sum())
if __name__ == '__main__':
t1 = time.time()
a = A()
a.run()
t2 = time.time()
print("cost time :{}".format(t2 - t1))
运行结果:
计算逻辑A
计算逻辑B
a的值为:10
b的值为:6
16
cost time :8.001021385192871
简单是使用 multiprocessing 对代码进行改造:
# –*– coding: utf-8 –*–
# @Time : 2019/3/19 21:23
# @Author : Damon_duanlei
# @FileName : process_01.py
# @BlogsAddr : https://blog.csdn.net/Damon_duanlei
import multiprocessing
import time
class A(object):
def __init__(self):
self.a = None
self.b = None
def get_num_a(self):
time.sleep(3)
self.a = 10
def get_num_b(self):
time.sleep(5)
self.b = 10
def sum(self):
print("a的值为:{}".format(self.a))
print("b的值为:{}".format(self.b))
ret = self.a + self.b
return ret
def run(self):
p1 = multiprocessing.Process(target=self.get_num_a)
p2 = multiprocessing.Process(target=self.get_num_b)
p1.start()
p2.start()
p1.join()
p2.join()
print(self.sum())
if __name__ == '__main__':
t1 = time.time()
a = A()
a.run()
t2 = time.time()
print("cost time :{}".format(t2 - t1))
运行结果:
a的值为:None
Traceback (most recent call last):
b的值为:None
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
因子进程与主进程不共享变量,所以主进程无法使用子进程中计算得到的数据 self.a和 self.b .了解多进程的同学都知道进程间通讯要借住 管道, 队列或共享内存, 比较之下我使用共享内存中的字典(multiprocessing.Manager().dict()) 将子进程中计算的数据以键值对的形式传入共享字典,在回到主进程中从字典取值并赋值给对应的类属性, 方便后续使用. 代码如下:
# –*– coding: utf-8 –*–
# @Time : 2019/3/19 22:58
# @Author : Damon_duanlei
# @FileName : process_02.py
# @BlogsAddr : https://blog.csdn.net/Damon_duanlei
import multiprocessing
import time
class A(object):
def __init__(self):
self.a = None
self.b = None
# 初始化一个共享字典
self.my_dict = multiprocessing.Manager().dict()
def get_num_a(self):
time.sleep(3)
self.my_dict["a"] = 10
def get_num_b(self):
time.sleep(5)
self.my_dict["b"] = 6
def sum(self):
self.a = self.my_dict["a"]
self.b = self.my_dict["b"]
print("a的值为:{}".format(self.a))
print("b的值为:{}".format(self.b))
ret = self.a + self.b
return ret
def run(self):
p1 = multiprocessing.Process(target=self.get_num_a)
p2 = multiprocessing.Process(target=self.get_num_b)
p1.start()
p2.start()
p1.join()
p2.join()
print(self.sum())
if __name__ == '__main__':
t1 = time.time()
a = A()
a.run()
t2 = time.time()
print("cost time :{}".format(t2 - t1))
运行结果:
a的值为:10
b的值为:6
16
cost time :5.442455053329468
如果各位小伙伴还有更好的方法请不吝赐教