python版本:3.7.10
- 进程池启动需要从主进入
__name__=="__mian__'
- 进程池的future任务函数不能用装饰器
使用示例:
def consume(a=1):
def wrapper(func):
def _inner(*args, **kwargs):
import time
s = time.time()
func(*args, **kwargs)
e = time.time()
print(f'{a}耗时:', e - s)
return _inner
return wrapper
@consume
def te(times):
time.sleep(times)
import os #获取pid必须在局部导入os
pid = os.getpid()
print(f'pid:{pid}被装饰函数{times}')
def tes(b):
te(b)
if __name__ == '__main__':#进程池要求这样做
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
from concurrent.futures import ProcessPoolExecutor, Executor
import multiprocessing
cup_num = multiprocessing.cpu_count()
print(cup_num)
# exec = ThreadPoolExecutor(max_workers=2, thread_name_prefix='pythonThreadPool')
# exec = ProcessPoolExecutor(max_workers=cup_num)
# with ThreadPoolExecutor(max_workers=cup_num) as exec:
with ProcessPoolExecutor(max_workers=cup_num) as exec:
exec.map(te, range(10))
# tasks = [exec.submit(te, i) for i in range(10)]
# wait(tasks, return_when=ALL_COMPLETED)
# exec.shutdown()
网友评论