我想监视跨不同进程的多个工作人员的进度。对于每个子流程,我都有自己的进度条,但是它不能与ProcessPoolExecutor
执行程序一起正常工作。
def main():
with futures.ProcessPoolExecutor(max_workers=PROCESSES) as executor:
fut_to_num = {}
for i in range(PROCESSES):
fut = executor.submit(execute_many_threads, i)
fut_to_num[fut] = i
for future in futures.as_completed(fut_to_num):
r = future.result()
# print('{} returned {}'.format(fut_to_num[future], r))
print('\nDone!\n')
def execute_many_threads(n_pool=0):
with futures.ThreadPoolExecutor(max_workers=THREADS) as executor:
for i in range(THREADS):
executor.submit(execute_thread, n_pool, i)
return n_pool+1
def execute_thread(n_pool=0, n_thread=0):
s = random.randint(1, 5)
thread_num = n_pool*(PROCESSES-1) + n_thread
progress = tqdm.tqdm(
desc='#{:02d}'.format(thread_num),
position=thread_num,
total=10*s,
leave=False,
)
# print('Executing {}: {}...'.format(thread_num, s))
for i in range(s):
time.sleep(1)
progress.update(n=10)
progress.close()
return s
当用ThreadPoolExecutor
(第二行)替换时,一切正常,因此我认为这可能与某些序列化问题有关。使用多处理程序时,能否帮助您找出tqdm
的正确调用。
python --version
是Python 3.7.2
和tqdm==4.31.1
,Ubuntu 18.04