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.0.tar.gz (4.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_runner-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: parallel-runner-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 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.0.tar.gz
Algorithm Hash digest
SHA256 e4a221b27ab5c0fc450ddc7cfe234677804dfeafe83a6ae746a0942bc5aff1de
MD5 4f08220ac46dd675587516d429cfa4e5
BLAKE2b-256 0f994f87f5cbd2006486ca78de9e692e13a9b1d45fefe834c81fb65695e1c101

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_runner-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9cebdbc4dd0f9ad47cc282aebcbd937b29a5106bb0bd3ba13169be0557233017
MD5 adff61414c60885c7cff95c3b0946b54
BLAKE2b-256 ff65fc294121290fc102882586b4d5b7973944c1da45f66dc675ab1ef724ca09

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