Skip to main content

Parallize is a Python package that provides utilities to parallelize both synchronous and asynchronous functions using the `concurrent.futures.ProcessPoolExecutor`.

Project description

Parallize

Parallize is a Python package that provides utilities to parallelize synchronous functions using the concurrent.futures.ProcessPoolExecutor and convert it into async functions to prevent the main thread from blocking. This allows you to execute functions in separate processes, leveraging multiple CPU cores for improved performance.

Features

  • Parallelize Synchronous Functions: Execute synchronous functions in parallel using multiple processes.
  • Parallelize Asynchronous Functions: Execute asynchronous functions in parallel using multiple processes.
  • Customizable Worker Count: Specify the maximum number of worker processes to use, or let the package use the number of available CPU cores by default.

Mini Benchmark

Here are the updated benchmark results comparing serial and parallel execution times using the test_aparallize.py test case:

Test Case Concurrent Execution Time Parallel Execution Time Speedup Tasks Count
test_aparallize_fn 0:00:17.215937 0:00:08.293026 2.08x 2
test_aparallize_10 0:01:25.070893 0:00:13.997451 5.94x 10

Benchmark Details

  • Concurrent Execution Time: The time taken to execute the CPU-bound task concurrently using a ThreadPoolExecutor.
  • Parallel Execution Time: The time taken to execute the same task in parallel using the aparallize decorator.
  • Speedup: The ratio of serial execution time to parallel execution time, indicating the performance improvement achieved by parallelizing the task.

Installation

To install Parallize, you can use pip:

pip install parallize

Usage

Parallelizing Synchronous Functions Using Decorator

To parallelize a synchronous function, use the parallize decorator:

from parallize import parallize

@parallize
def my_function(x, y):
    return x + y

# Call the function as usual
result = my_function(1, 2)
print(result)  # This will be executed in a separate process

Parallelizing Asynchronous Functions Using Decorator

To parallelize an asynchronous function, use the aparallize decorator:

import asyncio
from parallize import aparallize

@aparallize
async def my_async_function(x, y):
    await asyncio.sleep(1)
    return x + y

# Call the function as usual
result = my_async_function(1, 2)
print(result)  # This will be executed in a separate process

Another Usage Without Decorator

from parallize import parallize

def my_function(x, y):
    return x + y

# Call the function as usual
result = parallize(my_function)(1, 2)
print(result)  # This will be executed in a separate process

Customizing the Number of Workers

You can specify the maximum number of worker processes to use by passing the max_workers argument to the decorator:

@parallize(max_workers=4)
def my_function(x, y):
    return x + y

@aparallize(max_workers=4)
async def my_async_function(x, y):
    await asyncio.sleep(1)
    return x + y

License

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

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues

If you encounter any issues or have suggestions for improvements, please open an issue on the GitHub repository.

Acknowledgments

  • Thanks to the Python community for providing powerful tools and libraries for parallel processing.

@vikyw89-20240804

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

parallize-0.1.4.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

parallize-0.1.4-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file parallize-0.1.4.tar.gz.

File metadata

  • Download URL: parallize-0.1.4.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-39-generic

File hashes

Hashes for parallize-0.1.4.tar.gz
Algorithm Hash digest
SHA256 bfd8d661f96d3fe31140036809c4f6649005f85ff4c134d57d5f0b75cc4d0244
MD5 580642ffad67ac2da1c55477e92bd333
BLAKE2b-256 1bdb479184ddca934bca641bf859c523189d522f5faa4c6858e76f62a9282e56

See more details on using hashes here.

File details

Details for the file parallize-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: parallize-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-39-generic

File hashes

Hashes for parallize-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d0f3ae90df9d3d09767b404b492cf5c47be8d61ea7ac9c167aeb74e3a009199a
MD5 fa84e8469896a3fc7da9b791265eee94
BLAKE2b-256 3d5fad1eb7de5fe8cdb0eb8e12f87118dba176420cca74dcb0d929362bb73509

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page