Skip to main content

foreach

foreach is a tiny Python package that provides a parallel for loop implementation using multiprocessing.

Installation

You can install the package using pip:

pip install foreach

Usage

from foreach import foreach

def square(x):
    return x**2

params = [1, 2, 3, 4, 5]
result = foreach(square, params)
print(result)
# [1, 4, 9, 16, 25]

foreach Parameters

foreach(func: Callable, params: Iterable, parallel: bool = True, num_processes: int = None, callback: Callable = None) -> List
  • func (callable): The function to apply to each element.
  • params (iterable): The iterable of parameters to apply the function to.
  • parallel (bool): If True, use multiprocessing for parallel execution. Default is True.
  • num_processes (int): Number of processes to use in parallel execution (default is None, letting Pool decide).
  • callback (callable): Optional callback function to execute after each iteration.

Returns:

  • List: A list of results from applying the function to each element.

Examples

Sequential Execution

from foreach import foreach

def square(x):
    return x**2

params = [1, 2, 3, 4, 5]
result = foreach(square, params, parallel=False)
print(result)
# [1, 4, 9, 16, 25]

Callback Functionality

from foreach import foreach

def square(x):
    return x**2

params = [1, 2, 3, 4, 5]

def callback(x):
    print(f"Processed: {x}")

result = foreach(square, params, callback=callback)
# Processed: 1
# Processed: 4
# Processed: 9
# Processed: 16
# Processed: 25

Customizing Number of Processes

from foreach import foreach

def square(x):
    return x**2

params = [1, 2, 3, 4, 5]
result = foreach(square, params, num_processes=2)
print(result)
# [1, 4, 9, 16, 25]

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for foreach 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for foreach 0.4.0
File Size Uploaded Provenance Actions
foreach-0.4.0.tar.gz 2.5 kB Without provenance Details for foreach-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for foreach 0.4.0
File Interpreter ABI Platform Actions
foreach-0.4.0-py3-none-any.whl Python 3 none any Details for foreach-0.4.0-py3-none-any.whl

Total release size: 5.7 kB

Release files / foreach-0.4.0.tar.gz

Download URL foreach-0.4.0.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d7419a805800ee603f1297928d20355596f677ae58c05f3731304066912fec12
BLAKE2b-256 checksum
How to use checksums
50b548868df0e4393795ae432d41d277df187ddd9ddeb2187e254a2d07a20506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Linux/6.5.0-26-generic

Release files / foreach-0.4.0-py3-none-any.whl

Download URL foreach-0.4.0-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2fd68a5fd03842604ed608ae478690b1c894075988d7c97b10d673f490d7d879
BLAKE2b-256 checksum
How to use checksums
f65a1a643844084e8357bec2bc996026431147524a729ae9b8b71ab7a7e0b27b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.6 Linux/6.5.0-26-generic

Security for foreach 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page