Skip to main content

Python wrappers for easy multiprocessing and threading

Project description

parallel-execute

Lightweight Python wrappers for easy multiprocessing and multithreading.

Run multiple functions in parallel using a simple API built on top of threading or multiprocessing. Supports error handling, result tracking, timeouts, and controlled concurrency.

PyPI Downloads License Docs PyPI version PyPI wheel Python versions

Installation

Install the package using pip:

pip install parallel-execute

Usage Example

1. Create a Loom

A Loom takes multiple tasks (functions) and executes them in parallel using either threads or processes.

Using Threads:

from pexecute.thread import ThreadLoom
loom = ThreadLoom(max_runner_cap=10)

Using Processes:

from pexecute.process import ProcessLoom
loom = ProcessLoom(max_runner_cap=10)
  • max_runner_cap: The maximum number of threads/processes to run in parallel. You can queue as many functions as needed; only max_runner_cap will run at the same time.

2. Add Tasks to the Loom

Add a Single Task:

Use add_function to add individual functions:

loom.add_function(f1, args1, kw1)
loom.add_function(f2, args2, kw2)
loom.add_function(f3, args3, kw3)

Add Multiple Tasks at Once:

Use add_work to add a batch of functions:

work = [(f1, args1, kwargs1), (f2, args2, kwargs2), (f3, args3, kwargs3)]
loom.add_work(work)

3. Execute Tasks

Once all tasks are added, call execute() to run them. It returns a dictionary mapping each task to its result.

output = loom.execute()

By default, the keys are integers in the order the functions were added. Each value is a dictionary containing the result and execution metadata.

Example:

def fun1():
    return "Hello World"

def fun2(a):
    return a

def fun3(a, b=0):
    return a + b

loom.add_function(fun1, [], {})
loom.add_function(fun2, [1], {})
loom.add_function(fun3, [1], {'b': 3})

output = loom.execute()
>>> output
{
  0: {
      'output': 'Hello World',
      'got_error': False,
      'error': None,
      'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 395002),
      'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396500),
      'execution_time': 0.001498,
     },
  1: {
      'output': 1,
      'got_error': False,
      'error': None,
      'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396590),
      'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 397651),
      'execution_time': 0.001061
     },
  2: {
      'output': 4,
      'got_error': False,
      'error': None,
      'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 400323),
      'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 401749),
      'execution_time': 0.001426
     }
}

4. Using Custom Keys

You can assign a custom key to each task. This allows you to identify results more explicitly in the output dictionary.

loom.add_function(fun1, [], {}, 'key1')
loom.add_function(fun2, [1], {}, 'fun2')
loom.add_function(fun3, [1], {'b': 3}, 'xyz')

output = loom.execute()
>>> output
{
  'key1': {
      'output': 'Hello World',
      'got_error': False,
      'error': None,
      'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 395002),
      'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396500),
      'execution_time': 0.001498,
     },
  'fun2': {
      'output': 1,
      'got_error': False,
      'error': None,
      'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396590),
      'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 397651),
      'execution_time': 0.001061
     },
  'xyz': {
      'output': 4,
      'got_error': False,
      'error': None,
      'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 400323),
      'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 401749),
      'execution_time': 0.001426
     }
}

Migration Notice

parallel-execute is now powered by a newer, more powerful backend called concurra.

New users are encouraged to switch to the new interface:

from concurra import TaskRunner

runner = TaskRunner()
runner.add_task(my_func, *args, **kwargs)
results = runner.run()

Backward compatibility with ThreadLoom and ProcessLoom is currently maintained, but may be deprecated in future versions.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parallel_execute-2.0.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

parallel_execute-2.0.3-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file parallel_execute-2.0.3.tar.gz.

File metadata

  • Download URL: parallel_execute-2.0.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for parallel_execute-2.0.3.tar.gz
Algorithm Hash digest
SHA256 a8deae6233b657733036ea8632a7a7525a24169d90a94da533d64f7bdf157276
MD5 a3db245638ef6529ee5fa27e1014c7f2
BLAKE2b-256 31c2c5643d1ed5ce6f867566db55ca9a4a925692e3b31b4310f4f9bcea9185fa

See more details on using hashes here.

File details

Details for the file parallel_execute-2.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for parallel_execute-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 57e574fe5f1d7c7879375712087beb4ce3de6282ed84044fb15eeaa523854d2c
MD5 603c883662c2cf9b40963a07a2383d23
BLAKE2b-256 00f2d4ca09aa00eb778c75cb4e90cd42e56f5af98cf22221e19aaea7f8c801e0

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