Skip to main content

ThreadPool with unlimited workers count.

Project description

Unbounded Thread Pool

Implementation of python's concurrent.futures.Executor that creates new threads on demand and stops them if they are not needed anymore.

It is designed to allow infinite recursive submitting of tasks, so the following code works properly, despite creating of 10k threads:

from unbounded_thread_pool import UnboundedThreadPoolExecutor

with UnboundedThreadPoolExecutor() as executor:
    def factorial(n: int):
        if n == 0 or n == 1:
            return 1
        else:
            return executor.submit(factorial, n - 1).result() * n
    print(factorial(10000))

Installation

pip install unbounded_thread_pool

Requirements

  • Python (3.6, 3.7, 3.8)

Usage

UnboundedThreadPoolExecutor supports the following constructor parameters:

  • name: str: Name of executor, all thread names are prefixed with it. Default is UnboundedThreadPoolExecutor.
  • max_thread_idle_time: float: How many seconds idling worker thread should live. Default is 30 seconds.

For more details about methods check official python docs about concurrent.futures.Executor.

Usage with asyncio

When you write a lot of sync code that calls async code and vice versa (for example, if you use asgiref sync_to_async and async_to_sync) default asyncio executor can stuck. It happens in case you have the following code flow:

async code ---> sync code (1) ---> async code ---> sync code(2)

Here the second (2) sync code requires free worker to be available in thread pool, but thread pool can be already exhausted by the first (1) sync code. This causes deadlock, as (1) waits for (2), while (2) waits for (1) to free thread pool. To eliminate this issue, you can use UnboundedThreadPoolExecutor:

loop = asyncio.get_running_loop()
loop.set_default_executor(UnboundedThreadPoolExecutor(name='AsyncioExecutor'))

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

Built Distribution

unbounded_thread_pool-0.2.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file unbounded-thread-pool-0.2.0.linux-x86_64.tar.gz.

File metadata

  • Download URL: unbounded-thread-pool-0.2.0.linux-x86_64.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for unbounded-thread-pool-0.2.0.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 c2a4e9890b210ab3b6d9283cb4bb27e17f4d929c4b12447780edaceece838ea4
MD5 e552e216766ddca302a95da6e46680e5
BLAKE2b-256 8a39824bf5b8820114049c95b0f42f56d4c5a544fa8f6b9702f3ffb564853675

See more details on using hashes here.

File details

Details for the file unbounded_thread_pool-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: unbounded_thread_pool-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for unbounded_thread_pool-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd349bd0859ef98604b496e8131cec1a28ebfcb0afd844fbae9a1b1e6d7c3847
MD5 5bbd50e0e9aea1817f32eb5475f5b1c5
BLAKE2b-256 4abd7327623d16f52abb0d78281bc961726026f7944c6dfc12f762799b71fd30

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