Skip to main content

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.

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_bar doesn't need to know about the MultiProgress instance. The only time the two components will need to be aware of one another is the case of multiple instances of MultiProgress being used in the main process. In such cases you will use a key (a string) 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,
    ):
        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()}")


if __name__ == "__main__":
    demo()

asciicast

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

multiprog-0.1.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multiprog-0.1.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file multiprog-0.1.2.tar.gz.

File metadata

  • Download URL: multiprog-0.1.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.6

File hashes

Hashes for multiprog-0.1.2.tar.gz
Algorithm Hash digest
SHA256 22b2b06628208b271471b0ca3e5fa76c8948c85ec1e511759693607f7a94d874
MD5 1724ec2d50ce713960f2fa85424eb30a
BLAKE2b-256 f4875baeb51cfab8c8a0458c25ee148681c92481fd1c5e697fdfb5822e628997

See more details on using hashes here.

File details

Details for the file multiprog-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: multiprog-0.1.2-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

Hashes for multiprog-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2ebbfbccf4b955a20ddf8b898c5ca63a9be05cc88ee5ad73caeff4fef2ad37db
MD5 ff14bbc39d6870d48c8252b6c6474dee
BLAKE2b-256 b0621611d4cbce9a44f69dd25ba220b6e409c0e84f2e6a3709a3cb450f0be1de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page