python - 如何衡量 python 多处理 map_async 的进度?
问题描述
我曾经使用这个片段来监控多进程的进度。不过,现在 q.qsize() 停止工作并且只返回零。以前的做法有替代品q.qsize()
吗?或者有没有更好的方法来做到这一点?
from multiprocessing import Pool, Manager, cpu_count
def process(args):
'''
Pickleable function for (parallel) processing.
Expects a dictionary with keys:
Mandatory:
- 'filename': 'path to file to be processed',
...
Optional:
- 'queue': instance of multiprocessing.Manager().Queue()
'''
filename = args['filename']
mode = args['mode']
if 'queue' in args.keys():
q = args['queue']
q.put('filename')
return do_something_with(filename)
...
pool = Pool(processes=nthreads)
m = Manager()
q = m.Queue()
# prepare args...
results = pool.map_async(process, args)
# monitor progress
while True:
if results.ready():
break
else:
size = q.qsize()
self.progress = int(100 * (size / self.n_files))
time.sleep(1)
self.progress = 100
解决方案
推荐阅读
- flutter - 颤振异常:不能在“0”和“1”之间徘徊
- python-3.x - .remove 方法在 python 中有何不同?
- sql - 在 Microsoft SQL Server 中,是否可以以某种方式加速 Lag()?
- terraform - 仅将公共 IP 添加到一个 NIC (Azure)
- matlab - 将 CRC 初始移位寄存器值设置为全 1 的数学等效项
- sql-server - 错误:超出最大存储过程、函数、触发器或视图嵌套级别(限制 32)
- python - 传递自定义类型以单击 python
- kotlin - 我正在尝试以任何方式分发我的 TornadoFX 桌面应用程序(导出到 .jar 可执行文件或安装),但我似乎无法弄清楚如何
- python - 调试器不起作用:FileNotFoundError:[Errno 2] 没有这样的文件或目录:
- java - Apache-Pulsar Java 客户端可以在 RaspberryPi4 (ARMv8) 上运行吗?