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.
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-3.0.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84e84c66ecc7721afa043c523dc5cd4298d828f0ccdbb914fdafd6bf940665a5 |
|
MD5 | 04d9646aa2d4f44f6b17362ec6c0227c |
|
BLAKE2b-256 | 9470002f395bfa8ae159e14d431b100628f65c6ccbdde7c68742aea84fe1979e |
Hashes for threaded-3.0.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0177d4f94d82ded9910c9db5c9349c46f4cd8c86888b28b800f850bc976e7bf |
|
MD5 | 6b1885c387c57f8672e88b19e295eb17 |
|
BLAKE2b-256 | 351f36475d05679bffd1498d6122c55d694226b42ff3c68681d9022275e8a7c2 |
Hashes for threaded-3.0.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a833b5165221de5468316f866d77df7d7e9696272d04da32129f24811cd27425 |
|
MD5 | 9f248df52974b97ece228d26addb8ae9 |
|
BLAKE2b-256 | 9a8ba1294b04c3f1c99b2f44452fb19c61491a92167143b8f4caa0f73db27d45 |
Hashes for threaded-3.0.0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ea0418cd740a2ff6856ccc45ca2354150219a876c6a8bde2a424b28eb81145c |
|
MD5 | 6d4027b73ed484347c5ae70a0a3483b4 |
|
BLAKE2b-256 | f9bd9206dbdc96f898458c811c2cef81b6f8b0ba3a61c8a1af55b9573b51bdf7 |
Hashes for threaded-3.0.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4981e82c484742a0bb3cae6d6bd19bee7021ab0a8df0e8cf42b195dd3201b11f |
|
MD5 | b7f011a306ecbb225a4cb9f6cf46ea54 |
|
BLAKE2b-256 | 684b136f0a51944619f98f4dbe97f4f76504be04ceb76f5f0c1dff81cea15c4a |
Hashes for threaded-3.0.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d165f1725ecc7f6ff2787efb55b34132fffb731db4d95ccffa199bda4c411143 |
|
MD5 | 028e540c355b170fb6946e768aae18fb |
|
BLAKE2b-256 | 56aa3a01337cb77fcf36321ef93224615002f7aa71ea068dc362fca677df8f1b |
Hashes for threaded-3.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26c065d7a3eae960831a6a7b15ed9747c8d369cc969e7f84629521c402fd1dfc |
|
MD5 | 58a1196a46171b523c86c0208bb8e2a2 |
|
BLAKE2b-256 | 7af60002d487e0f3f12b45dda26fcc2071628cad40267ca673c59b527619d087 |
Hashes for threaded-3.0.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b936b366420fb958830c5015c6861e8db3fb8b7a88662fa67d44ec142c85e35a |
|
MD5 | 315f1f68042b0a2e1318d60604353c12 |
|
BLAKE2b-256 | 86ff4cc40063d05d7a344265f393a23e1c040b7f45a956e12ff6d53be68d141d |
Hashes for threaded-3.0.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 553c8a46b702d094e846177dee84ad24d2176e341ce3d0334b16f2f78abc0de8 |
|
MD5 | 1e1eb9202ad39d3e8617bc5a06c3ba61 |
|
BLAKE2b-256 | bff7ffde5cd28c234d151168c179c9997dc211fe6620070d33f8cba7ebe2863e |
Hashes for threaded-3.0.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9723ef028528a6af1895d395006253e43aa0c38e279a068f01bb6fe7a720783b |
|
MD5 | 5ca94dbbad00cd89727a2496ac2843c2 |
|
BLAKE2b-256 | 094c32f7c31c7e17fc9224e9d0feff8a077f75b07b9dfecafad333fce0095e8d |
Hashes for threaded-3.0.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b38cc7fa2271bd79a79b4eb883949fc5a3a50de7bb87fbada58064da0bf7cd9 |
|
MD5 | 936bf8d1320bc5b131a2d36372f5ee03 |
|
BLAKE2b-256 | 23d6cf0c5f81a9fee6054ea7720a01dcf4616297e1a5a112d6f3e4bc1ee1af8e |
Hashes for threaded-3.0.0-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63639949eb1900f9f213bcdb73e6e1568f4b4f28e657176b51374bc145b8e9ff |
|
MD5 | dbb4f982198507615473a3925854036c |
|
BLAKE2b-256 | 3b6d1a720a191ce65ad9658e0eee02ac0ef4bb13c886af85da826f7a97bd3fd3 |
Hashes for threaded-3.0.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f6f37936ba9f7c7b4c80b28e20008f1aaedae0f87125a50d1a7ee9b49d94f53 |
|
MD5 | dcca1c67c35f326f165050f5d8f11b9e |
|
BLAKE2b-256 | bb5864dc9ce1bec319336e2a51ab4ba3fae8f0fe0e6e4095e3efda4a513651e2 |
Hashes for threaded-3.0.0-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55ce92c193f4faeb6b071de359c4fbca819fceeb17679c1a440cf2ca34036a69 |
|
MD5 | c01b9aa46740e12f1ef07c5f1da8d6a9 |
|
BLAKE2b-256 | b115e2c978cef79adc88c0baa0438c108f3ffb10b734b298e3bf3433a02eb911 |