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.8-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 176e6ccb1f319105ff9bf562d51f42bef3446e112836541b7ad71fbdb8d169da |
|
MD5 | a5b711cd8595df74ce9eeef3af27342f |
|
BLAKE2b-256 | acbdfa65598d0e9089d9eb3539d6190a346d6c24543b9f0daa7ed472fddb73de |
Hashes for threaded-4.0.8-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f101a538f4eb5c49bf13dd3d81fc9e11fba8a7f6da9b1f72761e6f2667d04b6 |
|
MD5 | 3b0491fd0b9e8567e073d3558c24d64d |
|
BLAKE2b-256 | b21eaf1d313e2e8b179a70c578f2804aa188cb13efc6e59961c38bdddbc794bf |
Hashes for threaded-4.0.8-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2017b82c3861a9773ccbfa529cbb37dbace0d5c1cf99332485635f10cb7af88 |
|
MD5 | 4ced10d99311bbbea6f1b9f303ea5370 |
|
BLAKE2b-256 | 41c5f45d65de8f5ed00d198b0297a2cb0134d01c17859bd4e6e42dfa2aa45e2b |
Hashes for threaded-4.0.8-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a14d12ebf582bed90d2ce90cd75c1ff57f837ad0ab75d5efa94a78d0dbcb4f4 |
|
MD5 | be3a52a4d3c905a99af702b00e0108d1 |
|
BLAKE2b-256 | 1a82c49b839df7f9faee2683843fb31a63fb833b2a46717f30e2aab675f0d5eb |
Hashes for threaded-4.0.8-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac6655f21a2e5248f8616b49a75c93acca1c4f11778181df0a04ac490eb4359f |
|
MD5 | 28928962e314ee5fc781f6dbf55121bb |
|
BLAKE2b-256 | 1b2b9452e13c900705e5d6f73ec4fb00f266f7b7208b6e1bfd8968eec93cea81 |
Hashes for threaded-4.0.8-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5db8907215194eeb8daa97eadbd38f88ea1f2ec3bca25ca8e6db1725ad115c71 |
|
MD5 | 3350421018eb782eb5de7759fc5c6d8f |
|
BLAKE2b-256 | f8a683bde74fb135c1c0bf46aac374c88608173853802f9a675502f731df1b13 |
Hashes for threaded-4.0.8-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af1ffb73c0fa8020e8b9508f08744336409f197bf748fee399a9c984e1626185 |
|
MD5 | 727325ca0637e747aad2cdcfecea9c71 |
|
BLAKE2b-256 | 59b359b3ace223ce91ad90ad6681c19fc60eabdb66a01bed463ac5e9d314acf7 |
Hashes for threaded-4.0.8-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90a42b91afac2a8c1f15dfd5e1b7d389f7c938705c194a8e18118a5b69ee4701 |
|
MD5 | 55c154068968e3745646d3b1132534ed |
|
BLAKE2b-256 | 2e494e602991bd99859cbbd5ce6ed7ba5e1a3c1d15e0a28fe6c9c382dac22455 |
Hashes for threaded-4.0.8-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 317492f527098be4d9e9bde0ced05c6cf41114c5dc9b0d42ace32468bb99710a |
|
MD5 | 21cb580a81d70ffff2c680e3c34fc650 |
|
BLAKE2b-256 | c9ef7990a3484a7190b541b888ddbf8181db30dafc48bd8eaa07d6ef0fd5e985 |
Hashes for threaded-4.0.8-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b91c1e86eadc9753bd1ec33640543cc3caac3d9134cc72cfe63e1e325ffcc8e4 |
|
MD5 | 29153ac11bed6166d5a0c57c735ce08f |
|
BLAKE2b-256 | b94760c94474e267940ab92a7cc82d481cab47216c6375f7fed04fd3fca60fc5 |