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.
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.
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))
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.
Azure CI: is used for functional tests on Windows.
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-4.0.3-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 337789d144ca71c5f022053b0d2f2fec858e32f636051093e28f13da0afbb1cb |
|
MD5 | dd32aec68e6006a66f16b5f0b262f0e9 |
|
BLAKE2b-256 | 9aa89374007be70030f88185e602219d711f32fcab0cd35481f18f0d0adc86c4 |
Hashes for threaded-4.0.3-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24ebadb8f804e38a1355371a6b11fddf220cfaa8e104835bbbc435a7e59c9993 |
|
MD5 | 5b0782f79d35543d3c03fe77c3e38bac |
|
BLAKE2b-256 | 3262f2266448f267fc2bc0de15c077f835e2435bcdcad8db6f3df2d2aba14452 |
Hashes for threaded-4.0.3-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2ecf874b4e9db6efbe6b3bf4a5757336890499aeb318fb773a8235d48a23a8f |
|
MD5 | 69602c5aecf2e0252a21f2651eebd049 |
|
BLAKE2b-256 | c2aff5db5fe8bb26b48d595d98f43a97997a299b1d62d843e4570508d94acc3d |
Hashes for threaded-4.0.3-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d372a9a788f6e8a0d238092455ffbeb5df23e49c18e8bb0f13cad160d22fb055 |
|
MD5 | 7623df93ae2a0ddfdc92eca7a56ee87f |
|
BLAKE2b-256 | b3d02af368365db0a48e1a35452d2b717935e63d8ab40f9602b97f3207c229ca |
Hashes for threaded-4.0.3-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4e5837ae62b242ebb8f0c49dff17347cc9522a714a5569d416105a28ede3c9e |
|
MD5 | 7eddc6c2a7fda5ae83f5e66948f8660e |
|
BLAKE2b-256 | 40e4a7ef7f06964d6c1daadc8efbe7f0f768300b44e1a1be52dc93692c602e22 |
Hashes for threaded-4.0.3-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9822a4407961bed622339204771e0053121bf87d6fc76d77c87b06f503a55ce |
|
MD5 | 8da3dbc1583bbb6b0fe1c0728a973d06 |
|
BLAKE2b-256 | e497e688838d6fdf86d3e7f97eb2dcc19962ea5e6bf8589fb66968b4b3fa0954 |
Hashes for threaded-4.0.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2be93b1106439bd175702a5d912f42c525fc14ef510b58576361786cda598b86 |
|
MD5 | 0a046711888b92c9133e8b0819e63e96 |
|
BLAKE2b-256 | 792f07bb2a689fe73e113d696607e134416540eb843c7bd9f2cb8ee494fa9cd3 |
Hashes for threaded-4.0.3-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce72ae077299f1d77117c15ff58324fae06732a1520ecacc47dee9eb42432e0f |
|
MD5 | eb0dcc79a85434498a0959060a9fe1e5 |
|
BLAKE2b-256 | 91a5cd552f99b2c1084e201cf377d45436cd7359eedb80e369cc93a1a7cc7cd4 |