Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
The MIT License
Copyright (c) 2020 Mark Douthwaite
"""
import time
import multiprocessing as mp
from tqdm import tqdm
def my_process(pos: int) -> None:
"""Dummy process, where 'pos' is the position of the associated progress bar."""
with tqdm(desc=f"Process {pos}", total=100, position=pos) as progress:
for _ in range(100):
time.sleep(0.1)
progress.update(1)
if __name__ == "__main__":
n_cpu = mp.cpu_count()
# `tqdm` needs to get the global lock to avoid the progress bars 'jumping'
with mp.Pool(
processes=n_cpu, initializer=tqdm.set_lock, initargs=(tqdm.get_lock(),)
) as pool:
pool.map(my_process, range(n_cpu))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment