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

No additional installation is required apart from tqdm. Install it via:

pip install tqdm

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.1.tar.gz (4.3 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.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: parallel-runner-0.1.1.tar.gz
  • Upload date:
  • Size: 4.3 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.1.tar.gz
Algorithm Hash digest
SHA256 127bdade0716eb2a0f394bde9a123dcacf5ae9fe4f357128af11adcacdc4fc9f
MD5 6053172500c16b079150e3dc820b2051
BLAKE2b-256 4e51b4dae90ea601bb4c6084f61d8c5aa89e5d799629bc4b05f8fea2e3945b6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_runner-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a3fcee6431f48c118cdd78171cc89ae6e4ca1078a8f7aabdea6067702d86115c
MD5 0d9757d2d68bcfcdd21d7451362a2e5a
BLAKE2b-256 5e43bf282ac60c1411c43ac04f03fdd69942123cdf075dbf99a91d6350435a66

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