Skip to main content

tqdm-multiprocess

Using queues, tqdm-multiprocess supports multiple worker processes, each with multiple tqdm progress bars, displaying them cleanly through the main process.

It also redirects logging from the subprocesses to the root logger in the main process.

Currently doesn't support tqdm(iterator), you will need to intialize tqdm with a total and update manually.

Due to the performance limits of the default Python multiprocess queue you need to update your worker process tqdms infrequently to avoid flooding the main process. I will attempt to implement a lock free ringbuffer at some point to address this.

Installation

pip install tqdm-multiprocess

Usage

TqdmMultiProcessPool creates a standard python multiprocessing pool with the desired number of processes. Under the hood it uses async_apply with an event loop to monitor a tqdm and logging queue, allowing the worker processes to redirect both their tqdm objects and logging messages to your main process.

As shown below, you create a list of tasks containing their function and a tuple with your parameters. The functions you pass in will need an extra "tqdm_func" argument on the end which you must use to initialize your tqdms. As mentioned above, passing iterators into the tqdm function is currently not supported, so set total=total_steps when setting up your tqdm, and then update the progress manually with the update() method. All other arguments to tqdm should work fine.

Once you have your task list, call the map() method on your pool, passing in the process count, task list and error callback function. The error callback will be trigerred if your task functions return anything evaluating as False (if not task_result in the source code).

examples/basic_example.py

from time import sleep
import multiprocessing

import logging
from tqdm_multiprocess.logger import setup_logger_tqdm
logger = logging.getLogger(__name__)

from tqdm_multiprocess import TqdmMultiProcessPool

def some_other_function(tqdm_func):
    iterations1 = 100
    iterations2 = 5
    iterations3 = 2

    total_iterations = iterations1 * iterations2 * iterations3
    with tqdm_func(total=total_iterations, dynamic_ncols=True) as progress3:
        progress3.set_description("outer")
        for i in range(iterations3):
            logger.info("outer")
            total_iterations = iterations1 * iterations2
            with tqdm_func(total=total_iterations, dynamic_ncols=True) as progress2:
                progress2.set_description("middle")
                for j in range(iterations2):
                    logger.info("middle")
                    #for k in tqdm_func(range(iterations1), dynamic_ncols=True, desc="inner"):
                    with tqdm_func(total=iterations1, dynamic_ncols=True) as progress1:
                        for j in range(iterations1):
                            # logger.info("inner") # Spam slows down tqdm too much
                            progress1.set_description("innert")
                            sleep(0.01)
                            progress1.update()
                            progress2.update()
                            progress3.update()

    logger.warning(f"Warning test message. {multiprocessing.current_process().name}")
    logger.error(f"Error test message. {multiprocessing.current_process().name}")


# Multiprocessed
def example_multiprocessing_function(some_input, tqdm_func):  
    logger.debug(f"Debug test message - I won't show up in console. {multiprocessing.current_process().name}")
    logger.info(f"Info test message. {multiprocessing.current_process().name}")
    some_other_function(tqdm_func)
    return True

def error_callback():
    print("Error!")

def example():
    pool = TqdmMultiProcessPool()
    process_count = 4
    task_count = 10
    initial_tasks = [(example_multiprocessing_function, (i,)) for i in range(task_count)]    
    results = pool.map(process_count, initial_tasks, error_callback)
    print(results)

if __name__ == '__main__':
    logfile_path = "tqdm_multiprocessing_example.log"
    setup_logger_tqdm(logfile_path) # Logger will write messages using tqdm.write
    example()

Metadata

Release files for tqdm-multiprocess 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tqdm-multiprocess 0.0.5
File Size Uploaded
tqdm-multiprocess-0.0.5.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tqdm-multiprocess 0.0.5
File Interpreter ABI Platform
tqdm_multiprocess-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 16.8 kB

Release files / tqdm-multiprocess-0.0.5.tar.gz

Download URL tqdm-multiprocess-0.0.5.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ef885e80073064c60e929e2f4c1c85f6cea45ac079137357f0141621398f8fb2
BLAKE2b-256 checksum
How to use checksums
12a85a621d9403f4a7fe7357eaf6a71bc0762e6032a8fc3289cdb4bf1d47efd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release files / tqdm_multiprocess-0.0.5-py3-none-any.whl

Download URL tqdm_multiprocess-0.0.5-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e37ef5a43095122279abf0a2f3c66003a04e852a8194f87e5dd571c7b89a3f92
BLAKE2b-256 checksum
How to use checksums
357c7a96a9acff6f4a7f77d5dcdb934ee7c661e1230381ad61bd53f0f8a6c7f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release history Release notifications | RSS feed

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page