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-hypernode.com/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
File details
Details for the file threaded-3.0.3.tar.gz
.
File metadata
- Download URL: threaded-3.0.3.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad8652ff96f607e5433e1a869a333d6edb8d2a61b530d4bc586581c9f1af44da |
|
MD5 | b6c9a86554b550f92b20754e8f4b1fd5 |
|
BLAKE2b-256 | 3ec74a1439f6f041d0d7e49246e7190c1ff6c73630a885319943715ced40dbd1 |
File details
Details for the file threaded-3.0.3-py3-none-any.whl
.
File metadata
- Download URL: threaded-3.0.3-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.4.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e55079b90aad4c1538778082866b5d071d4d59e3888464c02a88746d1295ab6b |
|
MD5 | 6f1fb0eb9ff28476a721ce4a0832f5a7 |
|
BLAKE2b-256 | fcd3c2165b27405972797d57a14f50db047171d05f3066619b47c580b855ff79 |
File details
Details for the file threaded-3.0.3-cp35-cp35m-win_amd64.whl
.
File metadata
- Download URL: threaded-3.0.3-cp35-cp35m-win_amd64.whl
- Upload date:
- Size: 171.6 kB
- Tags: CPython 3.5m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a3febdaad853f5877b21f431c84e3d3ca967312c03d1c54d98901d78690697a |
|
MD5 | 752931c1ef82eab0fa171d81f1945b51 |
|
BLAKE2b-256 | ae6f1d4ed46d51137f37bd719f455b9a9b57a730d5bb516f5255ed99f23ab737 |
File details
Details for the file threaded-3.0.3-cp35-cp35m-win32.whl
.
File metadata
- Download URL: threaded-3.0.3-cp35-cp35m-win32.whl
- Upload date:
- Size: 145.8 kB
- Tags: CPython 3.5m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6924d2459d725bff5209f49e33bf49c9aefabc02ebdbbb64206fa63d6f4d9549 |
|
MD5 | d4511b5f17c074ba7c424b0b5f7447c4 |
|
BLAKE2b-256 | 55e8ca21ab18e3e19d48d17e5d257e85a66c21ecf5ba5863d9870f86c9ef1cf0 |
File details
Details for the file threaded-3.0.3-cp35-cp35m-manylinux1_x86_64.whl
.
File metadata
- Download URL: threaded-3.0.3-cp35-cp35m-manylinux1_x86_64.whl
- Upload date:
- Size: 657.3 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15cff3b86f18f145eab79b75fa4f04324e5ee72c0aa5c3a923bcd04838bdb4a7 |
|
MD5 | 34b86172b75bad131ac87efdbf81d9a6 |
|
BLAKE2b-256 | 019181a026721c7469e16206d2079582b58b42041f26476448fca1d5464f78d3 |
File details
Details for the file threaded-3.0.3-cp35-cp35m-manylinux1_i686.whl
.
File metadata
- Download URL: threaded-3.0.3-cp35-cp35m-manylinux1_i686.whl
- Upload date:
- Size: 604.1 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 154f2a9320915dc9363878dbd62dfa0adf705b3a2fcdc9ae8dd56ef99be6219e |
|
MD5 | 7969c9f2faa68e3d5c5530fb810677d4 |
|
BLAKE2b-256 | 98bcd205ed21aecb183a93316d6ff1d1bfa573d213ee55baacdabf706439bdc0 |
File details
Details for the file threaded-3.0.3-cp34-cp34m-manylinux1_x86_64.whl
.
File metadata
- Download URL: threaded-3.0.3-cp34-cp34m-manylinux1_x86_64.whl
- Upload date:
- Size: 621.5 kB
- Tags: CPython 3.4m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0317e9f90c4370e0a08aafdf4cabaa8ededdee457592f90c5d1dfcdd9c937462 |
|
MD5 | 0becc5daaa29a5aa8a046e5a6180fd86 |
|
BLAKE2b-256 | bf532a5d57ac28ab2b85818a8f082b3af82409b76271da0a1c1c6600914f51f9 |
File details
Details for the file threaded-3.0.3-cp34-cp34m-manylinux1_i686.whl
.
File metadata
- Download URL: threaded-3.0.3-cp34-cp34m-manylinux1_i686.whl
- Upload date:
- Size: 604.6 kB
- Tags: CPython 3.4m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d39462041ef3ca1980af2d03216cad23374ff333ecce729bee6a3a8a9cfb3bec |
|
MD5 | 9ac052b62ee84ae5a122a5eae1e2d2b6 |
|
BLAKE2b-256 | 9f3ea471c252f7609cdc79fb8b99952736504a904ba1a6cea92fae482c589bdc |