GithubHelp home page GithubHelp logo

ftpsolutions / collapsing-thread-pool-executor Goto Github PK

View Code? Open in Web Editor NEW
12.0 10.0 4.0 25 KB

A take on the concurrent.futures.thread.ThreadPoolExecutor that self-manages the number of threads

License: MIT License

Python 100.00%
python python3 threadpoolexecutor mit-license

collapsing-thread-pool-executor's Introduction

CollapsingThreadPoolExecutor

The CollapsingThreadPoolExecutor is inspired by and compatible with the ThreadPoolExecutor from the "futures" module, it operates differently in that worker threads are handled with a stack which results in the same worker or workers doing all the work (and idle workers being destroyed).

How to install

$ pip install collapsing-thread-pool-executor

How to develop

Prerequisites

  • python3 w/ pip
  • python2 w/ pip
  • virtualenvwrapper
  • entr

Set up the environments

$ mkvirtualenv -p `which python2.7` collapsing-thread-pool-executor-py2
$ pip install .
$ pip install -r requirements.txt

$ mkvirtualenv -p `which python3` collapsing-thread-pool-executor-py3
$ pip install .
$ pip install -r requirements.txt

Watch the tests

# watch python2 tests in one window
$ workon collapsing-thread-pool-executor-py2
$ find ./ -name '*.py' | entr -c py.test -v --log-level=DEBUG collapsing_thread_pool_executor

# watch python3 tests in one window
$ workon collapsing-thread-pool-executor-py3
$ find ./ -name '*.py' | entr -c py.test -v --log-level=DEBUG collapsing_thread_pool_executor

Examples

The example below will execute some_task() 100 times; as some_task() should take a second to execute and as we've allocated 10 workers, the whole thing should take about 10 seconds.

import time

from collapsing_thread_pool_executor import CollapsingThreadPoolExecutor

def some_task():
    time.sleep(1)

# all arguments are optional
pool = CollapsingThreadPoolExecutor(
    workers=10,
    thread_name_prefix='SomePool',
    permitted_thread_age_in_seconds=60,
)

for i in range(0, 100):
    pool.submit(some_task)

collapsing-thread-pool-executor's People

Contributors

aaltay avatar initialed85 avatar lowaa avatar robertwb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

collapsing-thread-pool-executor's Issues

Seems like worker threads are occasionally cleaned up before they're started

If there are more calls than workers submitted in the moment before the cleanup runs, it seems that the freshly created threads (to meet the increase in demand) are immediately cleaned up.

This shouldn't happen as I'm checking for thread.ident (to show that thread was ever started) as well as not thread.isAlive() (to show that thread is now stopped).

Need to dig into it.

'Future' object has no attribute 'set_exception_info' exception

CollapsingThreadPoolExecutor uses old set_exception_info method to set exception of Future instance. It isn't available in python3 standard library anymore:

  AttributeError: 'Future' object has no attribute 'set_exception_info'
  File "collapsing_thread_pool_executor/collapsing_thread_pool_executor.py", line 118, in run
    work_item.run()
  File "collapsing_thread_pool_executor/collapsing_thread_pool_executor.py", line 57, in run
    self.future.set_exception_info(e, tb)

(check https://docs.python.org/3.8/library/concurrent.futures.html#future-objects)

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.