Last active
January 15, 2022 16:44
-
-
Save markdouthwaite/4a33bf92d5ffdf264d7ee5938a31d84b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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