Skip to main content

A Python utility for running functions in parallel using multiprocessing.

Project description

ParallelRunner

Overview

ParallelRunner is a Python utility for executing functions in parallel using the multiprocessing module. It allows you to distribute workloads across multiple CPU cores, making it ideal for computationally intensive tasks.

Features:

  • Run functions in parallel with controlled concurrency.
  • Supports both normal and array-based arguments.
  • Optionally track progress using tqdm.
  • Handles errors gracefully without stopping execution.
  • Supports indexed execution if needed.

Installation

pip install parallel-runner

Usage

Basic Example

from parallel_runner import ParallelRunner

def sample_task(ix, x, y):
    return x + y + ix  # Example function that takes indexed arguments

runner = ParallelRunner(
    procedure=sample_task,
    concurrency=4,
    ix_needed=True,
    use_tqdm=True,
    array_args={"x": [1, 2, 3, 4], "y": [10, 20, 30, 40]},
)

results = runner.run()
print(results)  # Outputs: {0: 11, 1: 23, 2: 35, 3: 47}

Example: Processing Large Data Sets

import time
from parallel_runner import ParallelRunner

def process_data(data_chunk):
    time.sleep(1)  # Simulate a long computation
    return sum(data_chunk)

data = [[i for i in range(100)] for _ in range(10)]
runner = ParallelRunner(
    procedure=process_data,
    concurrency=5,
    array_args={"data_chunk": data},
)

results = runner.run()
print(results)  # Outputs sum of each chunk

Example: Web Scraping in Parallel

import requests
from parallel_runner import ParallelRunner

def fetch_page(ix, url):
    response = requests.get(url)
    return len(response.content)

urls = ["https://example.com" for _ in range(10)]
runner = ParallelRunner(
    procedure=fetch_page,
    concurrency=5,
    ix_needed=True,
    array_args={"url": urls},
)

results = runner.run()
print(results)  # Outputs content length for each page

Parameters

Parameter Type Description
procedure function The function to run in parallel.
debug bool If True, runs sequentially for debugging.
concurrency int Number of parallel processes.
ix_needed bool If True, passes an index (ix) to the function.
use_tqdm bool If True, displays a progress bar.
no_iteration int Number of iterations (if array_args is empty).
normal_args dict Arguments passed unchanged to all function calls.
array_args dict Arguments that vary per process (lists must have the same length).

License

This project is licensed under the MIT License.

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

parallel-runner-0.1.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

parallel_runner-0.1.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file parallel-runner-0.1.2.tar.gz.

File metadata

  • Download URL: parallel-runner-0.1.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for parallel-runner-0.1.2.tar.gz
Algorithm Hash digest
SHA256 34c8cf5b44d25c02bde918e198cea7b2d1d85578526276321ec0a70829b759c9
MD5 dfb0cd6b9e97bfb0232ca4390d16da31
BLAKE2b-256 7226d6f704f1f8d57e3cae4660a70ca9547aa85216d54b91b315a236d3e86592

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_runner-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 44e87aff2b0a41ebb81d30302629088da324086f6de9d311af455e5b2c828cca
MD5 176b94693055d3dcb604a928c4c856ac
BLAKE2b-256 a12cab98fe54d39ec77364143a347c51b1866d031fbb5735fd1d0392c5c873fd

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