Simple to use progress bar with subprocesses and threads.
Project description
Multiprogress
A simple to use progress_bar in sub-processes or threads using rich.
See the examples folder for more examples.
Install
pip install multiprog
Demo Video
Examples
Simple
import time
import random
from concurrent.futures import ProcessPoolExecutor
from multiprogress import MultiProgress, progress_bar
def do_work(n: int) -> int:
sleep_for = random.randint(0, 2)
for _ in progress_bar(
range(1, n + 2), desc=f"Sleeping for {sleep_for} secs for each {n} iterations."
):
time.sleep(sleep_for)
return sleep_for
def demo():
with ProcessPoolExecutor() as p, MultiProgress():
print(list(p.map(do_work, range(10))))
Using Other Rich Features
[!Note] The
progress_bardoesn't need to know about theMultiProgressinstance. The only time the two components will need to be aware of one another is in the case of multiple instances ofMultiProgressbeing used in the main process. In that case you will use akey(astring) to send progress updates to the correct instance.
import time
import random
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed
from multiprogress import MultiProgress, progress_bar
from itertools import chain
from rich import print
from rich.layout import Layout
from rich.panel import Panel
from rich.console import Group
from rich.live import Live
def do_work(n: int, key: str) -> int:
sleep_for = random.randint(0, 2)
for _ in progress_bar(
range(1, n + 2),
desc=f"Sleeping for {sleep_for} secs for each {n} iterations.",
key=key, # Used to specify which MultiProgress this will report to
):
time.sleep(sleep_for)
return n
def demo():
progress_p = MultiProgress(key="process", live_mode=False, transient=True)
progress_t = MultiProgress(key="thread", live_mode=False, transient=True)
layout = Layout()
layout.split_column(
Layout(Panel(progress_p, title="processes"), name="top"),
Layout(Panel(progress_t, title="threads"), name="bottom"),
)
with ThreadPoolExecutor() as t, ProcessPoolExecutor() as p, progress_p, progress_t, Live(
layout
):
p_futures = [p.submit(do_work, i, "process") for i in range(1, 10)]
t_futures = [t.submit(do_work, i, "thread") for i in range(1, 10)]
for f in as_completed(chain(p_futures, t_futures)):
print(f"Done processing {f.result()}")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multiprog-0.1.0.tar.gz.
File metadata
- Download URL: multiprog-0.1.0.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f32fad121ae140241b4d2148ec3cecdea2890758152eda294f3707e4b6f3a23
|
|
| MD5 |
0d2005019d6208645ecb7c81f0c12a6b
|
|
| BLAKE2b-256 |
e8c396ebfdc2a6f0cb8646faa3a5d94c63f01c67e3b1f53ed2c0d393816b9262
|
File details
Details for the file multiprog-0.1.0-py3-none-any.whl.
File metadata
- Download URL: multiprog-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69169ec8df85c7ab4f338a587877a035885616583cde5bd430eabfe52a9d2ca3
|
|
| MD5 |
6b46d45b3191caf9fcb002fe56b9c710
|
|
| BLAKE2b-256 |
6bd7e98c5862aeaff727753b6cb80fbc91d62270b2793ee77badf80641087211
|