加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pool_example.py 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
Tsingwaa 提交于 2022-01-25 22:27 . add fetch filenams
"""This is a template for multiprocessing.Pool usage.
"""
import time
# import json
# import os
import warnings
from multiprocessing import Pool
###############################################################################
# Setup global variables
INPUT_PATH = ''
OUTPUT_PATH = ''
PROCESSES = 40
PRINT_FREQ = 1000
CNT = 0
###############################################################################
# Setup the global handle to load inputs and save result.
inputs = [i for i in range(100)] # Usually from INPUT_PATH
results = []
###############################################################################
# Define main function for multiprocessing work.
def func_(input_):
return input_
###############################################################################
# Define callback function for *Only*-1 process to deal with the output of func
def callback_(output_):
"""
When each func_ is processed, this function is called to handle the output_
one by one.
"""
global results
global CNT
global PRINT_FREQ
CNT += 1
if CNT % PRINT_FREQ == 0:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
f"Handled {CNT}.")
results.append(output_)
if len(results) % (PRINT_FREQ * 5) == 0:
print(output_)
if __name__ == "__main__":
warnings.filterwarnings('ignore')
print(f"===> Start working with {PROCESSES} processes...")
pool = Pool(PROCESSES)
""" Method No.1: if no need for callback."""
# pool.map_async(func_, inputs, chunksize=PROCESSES, callback=callback_)
""" Method No.2: if need for callback and load progress bar."""
for input_ in inputs:
pool.apply_async(func_, (input_, ), callback=callback_)
pool.close()
pool.join()
# Save results.
with open(OUTPUT_PATH, 'w') as f_w:
f_w.writelines(results) # results is list type.
print(f"===> Done. Result is saved at '{OUTPUT_PATH}'.")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化