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.
GitHub actions: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests.
coveralls: is used for coverage display.
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.9-cp39-cp39m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 766d95bfedb5e7050b3492d1aabaaa7dd93a521fcc3ed88837e6494be61f624d |
|
MD5 | 3ac1c25229f4538d739e5e5ebd2823d8 |
|
BLAKE2b-256 | ca268f32d58c2a05030c197d60291ec9d6fd82f3a1f10cd159fc9353f98a4f21 |
Hashes for threaded-4.0.9-cp39-cp39m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11edb35d1ff1c3e42d13563e77f0b2d23f239ba7a6cb0112aaf227dd50794ac8 |
|
MD5 | 973ad43a1ac08db24ca535f848e97bc7 |
|
BLAKE2b-256 | 4f4afc8afbd18d1e96346f68bfb523772a30b13df781efbf2f4f02951727a658 |
Hashes for threaded-4.0.9-cp38-cp38m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73b59edec3e11f91b9da5f03665251067e035b8bd8fe4106161bdda70bfc71ab |
|
MD5 | f66b30585bcbbb2ebacd73a8f7a21955 |
|
BLAKE2b-256 | 689d25af508010754a387a0fab9c3b8b69badb7e6c8b3fc862deabf0c0717ffd |
Hashes for threaded-4.0.9-cp38-cp38m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daf57cfd8f0e9b0148d9ff6609f959bf3b7a42459bd85840fc6d56b441a407c6 |
|
MD5 | 88bdc5768828e320db359429a1ff54a7 |
|
BLAKE2b-256 | cdf63fba704242b61d4cd75a3c03bb46cc83cebf1e1f836e230ecd949e25bc1d |
Hashes for threaded-4.0.9-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bf0c9149aca0024bed1cd39f7ef85d9809e912a365e07ee035952e80a97ddfb |
|
MD5 | 97908e5fda87065df4f7f27e2bad334c |
|
BLAKE2b-256 | c6bf1a420590d9100238843ecfcb8779404acc635a5176cc9e808e8de447d277 |
Hashes for threaded-4.0.9-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fc4c6049fe0562e9b35b081d5b468949fb9047ed8dcd57272cf5c57b991a736 |
|
MD5 | 91ef92bf7f16cf42bd7cdad04a6734d7 |
|
BLAKE2b-256 | ec6e42bc29c356e818061639fa6fad0aac5d71c002edd5fa44f8c8fdb8730246 |