Skip to main content

Executors for both async and sync functions and automatically scales the number of workers

Project description

Flexexecutor

Testing Package version Python

Flexexecutor provides executors that can automatically scale the number of workers up and down.

Overview

Flexexecutor implements several subclasses of concurrent.futures.Executor. Like the built-in ThreadPoolExecutor and ProcessPoolExecutor, they create multiple workers to execute tasks concurrently. Additionally, it can shut down idle workers to save resources. These subclasses are:

  • ThreadPoolExecutor - thread concurrency, same name as the built-in class and can be a in-place replacement.
  • AsyncPoolExecutor - coroutine concurrency. Note that in flexexecutor's implementation, all coroutines are executed in a dedicated worker thread.
  • ProcessPoolExecutor - flexexecutor directly imports the built-in implementation as it already has scaling capabilities.

Features

  • Supports various concurrency modes: threads, processes and coroutines.
  • Automatically shut down idle workers to save resources.
  • Single file design, keeps the code clean and easy for hackers to directly take away and add more features.

Installation

Flexexecutor is available on PyPI:

pip install flexexecutor

Usage

ThreadPoolExecutor

from flexexecutor import ThreadPoolExecutor


def task(i):
    import time

    print(f"task {i} started")
    time.sleep(1)

if __name__ == "__main__":
    with ThreadPoolExecutor(
        # 1024 is the default value of max_workers, since workers are closed if they are
        # idle for some time, you can set it to a big value to get better short-term
        # performance.
        max_workers=1024,
        # Timeout for idle workers.
        idle_timeout=60.0,
        # These parameters are given for compatibility with the built-in
        # `ThreadPoolExecutor`, I don't use them very often, do you?
        thread_name_prefix="Task",
        initializer=None,
        initargs=(),
    ) as executor:
        for i in range(1024):
            executor.submit(task, i)

AsyncPoolExecutor

from flexexecutor import AsyncPoolExecutor


async def task(i):
    import asyncio

    print(f"task {i} started")
    await asyncio.sleep(1)

if __name__ == "__main__":
    # AsyncPoolExecutor behaves just like ThreadPoolExecutor except it only accepts
    # coroutine functions.
    with AsyncPoolExecutor(
        # Default value of max_workers is huge, if you don't like it, set it smaller.
        max_workers=1024,
        # Idle timeout for the working thread.
        idle_timeout=60.0,
        # These parameters are given for compatibility with the built-in
        # `ThreadPoolExecutor`, I don't use them very often, do you?
        thread_name_prefix="Task",
        initializer=None,
        initargs=(),
    ) as executor:
        for i in range(1024):
            executor.submit(task, i)

ProcessPoolExecutor

ProcessPoolExecutor in flexexecutor is just the same as the built-in concurrent.futures.ProcessPoolExecutor, we just import it directly for convenience.

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

flexexecutor-0.0.10.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

flexexecutor-0.0.10-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file flexexecutor-0.0.10.tar.gz.

File metadata

  • Download URL: flexexecutor-0.0.10.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.18.2.dev1+g8f0c5d5 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for flexexecutor-0.0.10.tar.gz
Algorithm Hash digest
SHA256 7378602c39783b7905cc77294ba7d7c90ac3e43ddc4e65a0c64c9fe2a6f8edc7
MD5 abb3052758017d543109fbf131466afa
BLAKE2b-256 d6fddd92b0154a6011673a605d7bf6ca00f58964913eeb7c6972332190ce4903

See more details on using hashes here.

File details

Details for the file flexexecutor-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: flexexecutor-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.18.2.dev1+g8f0c5d5 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for flexexecutor-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 0942c3abe76e7a8db3a4a43d67edeb0fdd15b67714d05fe673677c389befa1e6
MD5 a73692a35f310aa979e09c5cde7b2757
BLAKE2b-256 a6631801a3a287e05190c8395137c00ab3fd75c7138fd42bcfe55e0683d264d2

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