Giter Club home page Giter Club logo

unbounded-thread-pool's Introduction

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

unbounded-thread-pool's People

Stargazers

Ricardo Abuchaim avatar Roman Skurikhin avatar Gun avatar Elizabeth Popova avatar

Forkers

gungungun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.