every blog every motto: Light tomorrow with today.
网上目前关于多进程返回值的文章较为零散,本文主要进行简单的小结。
说明:
测试函数时间用到装饰器,具体可参考装饰器
导入模块:
import threading
from queue import Queue
import multiprocessing
from multiprocessing import Manager
import time
loop_number = 30000 # 循环次数
def fun(k):
"""被测试函数"""
print('-----fun函数,参数为{}----'.format(k))
m = k + 10
return m
@count_time
def call_fun():
"""没有使用多线/进程的情况"""
number = 0
for i in range(loop_number):
print('number:{}'.format(number))
number = fun(number)
def main():
"""主程序"""
# 1. 没有使用多线程
call_fun()
if __name__ == '__main__':
main()
有关多种进程池的apply,apply_async的时间比较可参考文献1
def fun(k):
"""被测试函数"""
print('-----fun函数,参数为{}----'.format(k))
m = k + 10
return m
@count_time
def my_process():
"""多进程"""
# 方法一:apply/apply_async
pool = multiprocessing.Pool(4) # 创建4个进程
k = 0
for i in range(loop_number):
k = pool.apply(fun, args=(k,)
print('返回值为:', k)
def main():
"""主程序"""
# 3. 使用多进程
my_process()
if __name__ == '__main__':
main()
def fun(k):
"""被测试函数"""
print('-----fun函数,参数为{}----'.format(k))
m = k + 10
return m
@count_time
def my_process():
"""多进程"""
# 方法一:apply/apply_async
pool = multiprocessing.Pool(4) # 创建4个进程
k = 0
for i in range(loop_number):
k = pool.apply_async(fun, args=(k,))
k = k.get()
print('返回值为:', k)
def main():
"""主程序"""
# 3. 使用多进程
my_process()
if __name__ == '__main__':
main()
结果:
说明:
上述运行时间,仅作一般参考,运行时间非本文重点,具体参考文献1
小结:
@count_time
def my_process():
"""多进程"""
# 方法一:apply/apply_async
pool = multiprocessing.Pool(4) # 创建4个进程
k = 0
for i in range(loop_number):
t = pool.apply_async(fun, args=(k,))
print('返回值为:', t.get())
@count_time
def my_process():
"""多进程"""
# 方法一:apply/apply_async
pool = multiprocessing.Pool(4) # 创建4个进程
k = 0
li = [] # 空列表
for i in range(loop_number):
t = pool.apply_async(fun, args=(k,))
li.append(t)
for i in li:
print('返回值为:', i.get())
在进行得到各个结果的时候,是将结果追加到列表中,否则,在得到结果get的时候会阻塞进程,从而将多进程变成了单进程,当使用列表来存放时,在进行get数据的时候,可以设置超时时间,如,get(timeout=5)。
小结:
from multiprocessing import Manager, Process
def fun(k,result_dict):
"""被测试函数"""
print('-----fun函数内部,参数为{}----'.format(k))
m = k + 10
result_dict[k] = m # 方法二:manger
@count_time
def my_process():
"""多进程"""
# 方法二:Manger
manger = Manager()
result_dict = manger.dict() # 使用字典
jobs = []
for i in range(10):
p = Process(target=fun, args=(i, result_dict))
jobs.append(p)
p.start()
for pr in jobs:
pr.join()
var = result_dict
print('返回结果', var.values())
def main():
"""主程序"""
# 3. 使用多进程
my_process()
if __name__ == '__main__':
main()
说明:
定义类
class MyProcess(multiprocessing.Process):
def __init__(self, name, func, args):
super(MyProcess, self).__init__()
self.name = name
self.func = func
self.args = args
self.res = ''
def run(self):
self.res = self.func(*self.args)
def fun(k,p):
"""被测试函数"""
print('-----fun函数内部,参数为{}----'.format(k))
m = k + 10
p.send(m)
@count_time
def my_process():
"""多进程"""
# 方法三:
process_li = []
parent_con, child_con = multiprocessing.Pipe()
for i in range(30):
p = MyProcess('proce', fun, (1, child_con))
process_li.append(p)
p.start()
for i in process_li:
i.join()
for i in process_li:
print(parent_con.recv())
定义类
class MyProcess(multiprocessing.Process):
def __init__(self, name, func, args):
super(MyProcess, self).__init__()
self.name = name
self.func = func
self.args = args
self.res = ''
def run(self):
self.res = self.func(*self.args)
def fun(k, p):
"""被测试函数"""
print('-----fun函数内部,参数为{}----'.format(k))
m = k + 10
p.put(m)
@count_time
def my_process():
"""多进程"""
# 方法四:Queue
process_li = []
q = multiprocessing.Queue()
k = 1
for i in range(30):
p = MyProcess('proce', fun, (k, q))
p.start()
process_li.append(p)
for i in process_li:
i.join()
while q.qsize() > 0:
print(q.get())
总结:
[1] https://blog.csdn.net/weixin_39190382/article/details/107865552
[2] https://zhidao.baidu.com/question/246068963774450124.html
[3] https://blog.csdn.net/sunt2018/article/details/85336408
[4] https://blog.csdn.net/ztf312/article/details/80337255
[5] https://www.jb51.net/article/86412.htm
[6] https://blog.csdn.net/littlehaes/article/details/102626610