Decorators for running functions in Thread/ThreadPool/IOLoop
Project description
threaded
threaded is a set of decorators, which wrap functions in:
concurrent.futures.ThreadPool
threading.Thread
asyncio.Task in Python 3.
gevent.threadpool.ThreadPool if gevent is installed.
Why? Because copy-paste of loop.create_task, threading.Thread and thread_pool.submit is boring, especially if target functions is used by this way only.
Pros:
Free software: Apache license
Open Source: https://github.com/python-useful-helpers/threaded
PyPI packaged: https://pypi.python.org/pypi/threaded
Tested: see bages on top
Support multiple Python versions:
Python 3.4 Python 3.5 Python 3.6 Python 3.7 PyPy3 3.5+
Decorators:
ThreadPooled - native concurrent.futures.ThreadPool.
threadpooled is alias for ThreadPooled.
Threaded - wrap in threading.Thread.
threaded is alias for Threaded.
AsyncIOTask - wrap in asyncio.Task. Uses the same API, as ThreadPooled.
asynciotask is alias for AsyncIOTask.
GThreadPooled - wrap function in gevent.threadpool.ThreadPool.
gthreadpooled is alias for GThreadPooled.
Usage
ThreadPooled
Mostly it is required decorator: submit function to ThreadPoolExecutor on call.
threaded.ThreadPooled.configure(max_workers=3)
@threaded.ThreadPooled
def func():
pass
concurrent.futures.wait([func()])
Python 3.5+ usage with asyncio:
loop = asyncio.get_event_loop()
@threaded.ThreadPooled(loop_getter=loop, loop_getter_need_context=False)
def func():
pass
loop.run_until_complete(asyncio.wait_for(func(), timeout))
Python 3.5+ usage with asyncio and loop extraction from call arguments:
loop_getter = lambda tgt_loop: tgt_loop
@threaded.ThreadPooled(loop_getter=loop_getter, loop_getter_need_context=True) # loop_getter_need_context is required
def func(*args, **kwargs):
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(func(loop), timeout))
During application shutdown, pool can be stopped (while it will be recreated automatically, if some component will request).
threaded.ThreadPooled.shutdown()
Threaded
Classic threading.Thread. Useful for running until close and self-closing threads without return.
Usage example:
@threaded.Threaded
def func(*args, **kwargs):
pass
thread = func()
thread.start()
thread.join()
Without arguments, thread name will use pattern: 'Threaded: ' + func.__name__
Override name can be don via corresponding argument:
@threaded.Threaded(name='Function in thread')
def func(*args, **kwargs):
pass
Thread can be daemonized automatically:
@threaded.Threaded(daemon=True)
def func(*args, **kwargs):
pass
Also, if no any addition manipulations expected before thread start, it can be started automatically before return:
@threaded.Threaded(started=True)
def func(*args, **kwargs):
pass
AsyncIOTask
Wrap in asyncio.Task.
usage with asyncio:
@threaded.AsyncIOTask
def func():
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(func(), timeout))
Provide event loop directly:
loop = asyncio.get_event_loop()
@threaded.AsyncIOTask(loop_getter=loop)
def func():
pass
loop.run_until_complete(asyncio.wait_for(func(), timeout))
Usage with loop extraction from call arguments:
loop_getter = lambda tgt_loop: tgt_loop
@threaded.AsyncIOTask(loop_getter=loop_getter, loop_getter_need_context=True)
def func(*args, **kwargs):
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(func(loop), timeout))
GThreadPooled
Post function to gevent.threadpool.ThreadPool.
threaded.GThreadPooled.configure(max_workers=3)
Basic usage example:
@threaded.GThreadPooled
def func():
pass
func().wait()
Testing
The main test mechanism for the package threaded is using tox. Available environments can be collected via tox -l
CI systems
For code checking several CI systems is used in parallel:
Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.
coveralls: is used for coverage display.
CD system
Travis CI: is used for package delivery on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for threaded-2.1.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 617d8d2d15b4eb59a122b1b3dfb5f4db3949ce9d9246d901720b4457281bdab1 |
|
MD5 | 07f187d4a0d7e525c04f6db46e7b95a1 |
|
BLAKE2b-256 | 968e1f411df55616cf3cc7bedfeb85f5eec5bafe921db8780870b5017103dc45 |
Hashes for threaded-2.1.1-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5b62dae796b8a9d2ecad9d1033ff644996b60399d738ea7f3b117f0a6091883 |
|
MD5 | b28474a13d3bade8a77401cbd787c48f |
|
BLAKE2b-256 | 4c9a39e11a22cf26579a791032c49adf90af5bbcf3f3fcf2d5a757d33f7ba0fe |
Hashes for threaded-2.1.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8a0d6bdc30c983d38fc46d2f3f0a8427d21182e63fd982b8fb7f16c8edbc965 |
|
MD5 | 548a77235ebf3aacb02de85ef729bcfc |
|
BLAKE2b-256 | 35b0ab5f01e39c8ff6522891d3e9e63d8bccac3bec29d11122398b3b60a73957 |
Hashes for threaded-2.1.1-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07e0fc92efd4191dc537b74a6deed74797bb402195217389eebfe60dfe41cd41 |
|
MD5 | 7bcb61606fd38f81956f400c461dc0aa |
|
BLAKE2b-256 | 4a0b460fb27ef6e1a079485a07883a4eb7935beeed85aec474cc95e77d6dfff9 |
Hashes for threaded-2.1.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b8f8fb2f1791df7d491a896eff8bc7fa808122b4fd49a60952be6b665a8b759 |
|
MD5 | 3e4a127b0195d9e066f86538cbbbebb8 |
|
BLAKE2b-256 | 9f236fb5eb7ac72cb6448faba2a52ea59a5291344f172c76f13cf7e49e6d8dc7 |
Hashes for threaded-2.1.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d5ad36769457505105c691a0fd8346d37dba667fe84ab98d0a3a104422709c5 |
|
MD5 | b6efb5bae4f93718b8e9d0765662fc8a |
|
BLAKE2b-256 | 29b2e7c9ffe104bfefca5cfa2ff13b03b83d9ea4d188409bdaa254d4f7bfcb17 |
Hashes for threaded-2.1.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e101d3c55fc8b8f48aea8862fc4302520a6d9c4e410334e02ce4821aafd4260b |
|
MD5 | 6d6acfb3acbbe68f821cde4e9cb0d109 |
|
BLAKE2b-256 | d8a13a8cf65b0573446d566baf2feb3584ece9667f9556b04d185c080bc92986 |
Hashes for threaded-2.1.1-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b4db5824295ee155d14cf9e19c49b824e02f0078450b93148ac2dbf7bfb415e |
|
MD5 | 9df69b3f9ab80cb451f7a736428d03e7 |
|
BLAKE2b-256 | 75fc3a387dc2c4614b75db174fd5a2e44e75826a0bccbac352f8ce948b6ed252 |