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.4-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 647269e3a1df9f91aa0f0f3f1a5558b0e3d4b33280fc6f41718c9a0a12cfb190 |
|
MD5 | fd95d0086ece27e172a229e8cf04a9dc |
|
BLAKE2b-256 | 9f7fae69a1c938f9eb347361a3191ba16e6d9aadfea160f1e64c2a94a13fede2 |
Hashes for threaded-4.0.4-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27a8275a9c803baad347d78c020000b20f1c9a4114604017877eacda190fc9f6 |
|
MD5 | 7cafdd8422bab131e01c12a9b84e928d |
|
BLAKE2b-256 | 5a8e16601b72b17dd0919556a8495828cb90674460133a4b5b3b94bfafa451dd |
Hashes for threaded-4.0.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6de8d8c42dfb388ffd400759dc36e1dbada79ecaec9cd7c0a06cf3d40032b8ee |
|
MD5 | 7bb2843c60b2960e08a9cbc5e1178ec9 |
|
BLAKE2b-256 | d12defa34a80aa951ebd0eb7f29011f9552bd60a0c358954cc876e28d7b05e3f |
Hashes for threaded-4.0.4-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a294af46c502fb981974be5a42cde5b3d3b6dca349ce5382df1b2b190ed033d |
|
MD5 | 93ffdde7200700d7599d178ef15dfff3 |
|
BLAKE2b-256 | cd41a8d1b4872131961bd9bd8ea1598aa301addca8557326e99cf86d61eb6b63 |