python线程池和进程池的多参数调用
Posted WILLPOWER-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python线程池和进程池的多参数调用相关的知识,希望对你有一定的参考价值。
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
def calctime(x):
start = time.perf_counter()
x()
end = time.perf_counter()
return("time cost is", end-start)
def fun(x):
a, b = x
time.delay(2)
return('a is ', a, 'b is ', b)
def muti_thread1():
with ThreadPoolExecutor() as pool:
data = [(x, x) for x in range(50)]
results = pool.map(fun, data)
def thread():
data = [(x, x) for x in range(50)]
for i in data:
fun(i)
def muti_process():
with ProcessPoolExecutor() as pool:
data = [(x, x) for x in range(50)]
results = pool.map(fun, data)
if __name__ == '__main__':
print(calctime(muti_thread1))
print(calctime(thread))
print(calctime(muti_process))
注意事项:
- 要注意在开启线程的程序中,一定要注意变量的使用是否是赋值、浅拷贝、还是深拷贝,则会影响到线程之间的变量共享使用等!
- 在开启多进程时候,必须要指定主程序__name__ == ‘main’
- 使用map是顺序输出,要想不顺序输出,可以使用as_completed
图片来源
以上是关于python线程池和进程池的多参数调用的主要内容,如果未能解决你的问题,请参考以下文章