Skip to content

Instantly share code, notes, and snippets.

@markdouthwaite
Last active January 15, 2022 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markdouthwaite/4a33bf92d5ffdf264d7ee5938a31d84b to your computer and use it in GitHub Desktop.
Save markdouthwaite/4a33bf92d5ffdf264d7ee5938a31d84b to your computer and use it in GitHub Desktop.
"""
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