Skip to main content

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'))

Metadata

Release files for unbounded-thread-pool 0.2.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 unbounded-thread-pool 0.2.0
File Size Uploaded
unbounded-thread-pool-0.2.0.linux-x86_64.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unbounded-thread-pool 0.2.0
File Interpreter ABI Platform
unbounded_thread_pool-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / unbounded-thread-pool-0.2.0.linux-x86_64.tar.gz

Download URL unbounded-thread-pool-0.2.0.linux-x86_64.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c2a4e9890b210ab3b6d9283cb4bb27e17f4d929c4b12447780edaceece838ea4
BLAKE2b-256 checksum
How to use checksums
8a39824bf5b8820114049c95b0f42f56d4c5a544fa8f6b9702f3ffb564853675
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / unbounded_thread_pool-0.2.0-py3-none-any.whl

Download URL unbounded_thread_pool-0.2.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd349bd0859ef98604b496e8131cec1a28ebfcb0afd844fbae9a1b1e6d7c3847
BLAKE2b-256 checksum
How to use checksums
4abd7327623d16f52abb0d78281bc961726026f7944c6dfc12f762799b71fd30
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

1 release file

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