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.5-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 519492cb4eb43c2d5dabc91ec39e26ed70781269862a32c141ddd57bd2bfdc97 |
|
MD5 | 3381a499ac845d8653e981b54d08fe9a |
|
BLAKE2b-256 | 8db8402b75acb5cda7efa9cd3713f9e80065e6c0e208a30f99075a7d05496394 |
Hashes for threaded-4.0.5-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 313ac1362832f2c7b0019d0f9100ebf00b208ff83c399ff871421c6125a9b048 |
|
MD5 | b0bb23466d018fe8ac555460d3453148 |
|
BLAKE2b-256 | 06ec29d9afdaca9de1ec6095b7c1203636bf3cf9cca264019723c6d326252b2b |
Hashes for threaded-4.0.5-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daebb2447036039f8a2500bec83c89c559473e5766195543a9471a4e52f58cfd |
|
MD5 | ff224aabf773249f9b555f4879db4063 |
|
BLAKE2b-256 | c5f3aef10f157d817850e4c6fcf25fb93bbc601a81fbbf5a768a9beef9a1df1a |
Hashes for threaded-4.0.5-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29ebc5576ccd247b150a8410ecba0a0c93d24109bfdaf9c1de540b4a28b33047 |
|
MD5 | 0f03141949346b8cd90eed93c4316596 |
|
BLAKE2b-256 | 179f2aced5dbc53669a4791207618233dcd3c71c5661eca2ab0e2bcf31841143 |
Hashes for threaded-4.0.5-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b282b0638c0e84b712546c93a82ca12f32e25609f95b2754b279dbe0b8893a6 |
|
MD5 | ae905ed9799131fbb6916efc7203dce1 |
|
BLAKE2b-256 | a36327c1ff956cff9a4ac594e9d66bf38ed9c01765d0890959bbe9e8e532454f |
Hashes for threaded-4.0.5-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a966a7465ea9f7228e42b8b198c265b2f8fb89c6466be499629a590bfb87849 |
|
MD5 | 6958a92e06d205871eb0d32e7f3a9e96 |
|
BLAKE2b-256 | 77bd29df4bf122047d8a89b2db8c6f3fbf50ebdf5dd6a978aa9f83aeefe04dcb |
Hashes for threaded-4.0.5-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08a6e218beac965324e8e33f233ac1b634e6e18555e14922819465fe01d23d37 |
|
MD5 | 51fc0747406ca003464658792faf92bb |
|
BLAKE2b-256 | b9362178c5c1a2594b4786411ea63e74f4ae490beca5add8a04889e467507571 |
Hashes for threaded-4.0.5-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86f3f265d89ea52f0ea7d529f4b3740c0782db9520e5f5abbba67b4df7113751 |
|
MD5 | e06daaa70b728b2e6304896b679f5223 |
|
BLAKE2b-256 | f96cfaafebd5b0abc8f9bb6d392f1c6fb6c347a2eca3d48d9ad1e1150508447c |