Skip to main content

Multiprocessing with tqdm progressbars!

Project description

tqdmp

License MIT PyPI Python Version Unit Tests codecov

Multiprocessing with tqdm progressbars!

Did you feel like you are missing a progressbar when using doing multiprocessing with pool.map? Well no more! tqdmp is an easy to use wrapper for pool.map but with a tqdm progressbar. Moreover it supports some nice usecases making multiprocessing much simpler:

  • Use multiple iterables by simply packing them as a tuple
  • Unpack the function output of the parallized function in case it returns multiple variables
  • Switch to single processing by setting the number of processes to None or 0
  • Pass additional constant variables to the parallized function as kwargs

Examples of these usecases are given in section Examples.

Installation

You can install tqdmp via pip:

pip install tqdmp

Examples

In the following we demonstrate the general useage of tqdmp and some useful features.

Default multiprocessing

Using pool.map:

from multiprocessing import Pool

def function(index):
    return index * 10

pool = Pool(2)
result = pool.map(function, range(10))
print(f"pool.map: {result}")

Using tqdmp:

from tqdmp import tqdmp

def function(index):
    return index * 10

result = tqdmp(function, range(10), 2)
print(f"tqdmp: {result}")

Multiple iterables

Using pool.map:

from multiprocessing import Pool

def function(input_tuple):
    index1, index2 = input_tuple
    return index1 * index2

pool = Pool(2)
result = pool.map(function, zip(range(10), range(10)))
print(f"pool.map: {result}")

Using tqdmp:

from tqdmp import tqdmp

def function(index1, index2):
    return index1 * index2

result = tqdmp(function, (range(10), range(10)), 2, mult_iter=True)
print(f"tqdmp: {result}")

Multiple outputs

Using pool.map:

from multiprocessing import Pool

def function(index1):
    return index1, index1 * 10

pool = Pool(2)
result = pool.map(function, range(10))
result1, result2  = map(list, zip(*result))
print(f"pool.map result1: {result1}")
print(f"pool.map result2: {result2}")

Using tqdmp:

from tqdmp import tqdmp

def function(index1):
    return index1, index1 * 10

result1, result2 = tqdmp(function, range(10), 2, mult_out=True)
print(f"tqdmp result1: {result1}")
print(f"tqdmp result2: {result2}")

Switching between single- and multiprocessing

Using pool.map:

from multiprocessing import Pool

def function(index):
    return index * 10

num_processes = None  # None or 0 for single, >0 for multi
iterable = range(10)

if (num_processes is None) or (num_processes == 0):
    result = [function(index) for index in iterable]
else:
    pool = Pool(num_processes)
    result = pool.map(function, iterable)
print(f"pool.map: {result}")

Using tqdmp:

from tqdmp import tqdmp

def function(index):
    return index * 10

result = tqdmp(function, range(10), None)  # None or 0 for single, >0 for multi
print(f"tqdmp: {result}")

Passing additional constant variables

Using pool.map:

from multiprocessing import Pool
from functools import partial

def function(index, const1, const2):
    return (index * const1) + const2

pool = Pool(2)
partial_func = partial(function, const1=5, const2=7)
result = pool.map(partial_func, range(10))
print(f"pool.map: {result}")

Using tqdmp:

from tqdmp import tqdmp

def function(index, const1, const2):
    return (index * const1) + const2

result = tqdmp(function, range(10), 2, const1=5, const2=7)
print(f"tqdmp: {result}")

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "tqdmp" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

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

tqdmp-0.0.3.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

tqdmp-0.0.3-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file tqdmp-0.0.3.tar.gz.

File metadata

  • Download URL: tqdmp-0.0.3.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for tqdmp-0.0.3.tar.gz
Algorithm Hash digest
SHA256 0f61ad293b6d0f4a9940c14c670a003abddfe30023e914f81eb81128f0227c10
MD5 05f8f0857dc2b5bba2e4d50dcbd34b7d
BLAKE2b-256 81ea714796ea20800a75c1adbcc6452a3c06a393eb6d86be624898363f5054ba

See more details on using hashes here.

File details

Details for the file tqdmp-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: tqdmp-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for tqdmp-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 17a27f56ad8db566329fae8e6663bf78b7bde892f17cfaa6de0f80134d568fc9
MD5 2d7ea5e398f5e5b5176a255c8855d1a0
BLAKE2b-256 9b1aa4ab895fec65f65bc3ca892608782da8783a4b45c7e9b840512a905f9acd

See more details on using hashes here.

Supported by

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