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-3.0.3-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a3febdaad853f5877b21f431c84e3d3ca967312c03d1c54d98901d78690697a |
|
MD5 | 752931c1ef82eab0fa171d81f1945b51 |
|
BLAKE2b-256 | ae6f1d4ed46d51137f37bd719f455b9a9b57a730d5bb516f5255ed99f23ab737 |
Hashes for threaded-3.0.3-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6924d2459d725bff5209f49e33bf49c9aefabc02ebdbbb64206fa63d6f4d9549 |
|
MD5 | d4511b5f17c074ba7c424b0b5f7447c4 |
|
BLAKE2b-256 | 55e8ca21ab18e3e19d48d17e5d257e85a66c21ecf5ba5863d9870f86c9ef1cf0 |
Hashes for threaded-3.0.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15cff3b86f18f145eab79b75fa4f04324e5ee72c0aa5c3a923bcd04838bdb4a7 |
|
MD5 | 34b86172b75bad131ac87efdbf81d9a6 |
|
BLAKE2b-256 | 019181a026721c7469e16206d2079582b58b42041f26476448fca1d5464f78d3 |
Hashes for threaded-3.0.3-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 154f2a9320915dc9363878dbd62dfa0adf705b3a2fcdc9ae8dd56ef99be6219e |
|
MD5 | 7969c9f2faa68e3d5c5530fb810677d4 |
|
BLAKE2b-256 | 98bcd205ed21aecb183a93316d6ff1d1bfa573d213ee55baacdabf706439bdc0 |
Hashes for threaded-3.0.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0317e9f90c4370e0a08aafdf4cabaa8ededdee457592f90c5d1dfcdd9c937462 |
|
MD5 | 0becc5daaa29a5aa8a046e5a6180fd86 |
|
BLAKE2b-256 | bf532a5d57ac28ab2b85818a8f082b3af82409b76271da0a1c1c6600914f51f9 |
Hashes for threaded-3.0.3-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d39462041ef3ca1980af2d03216cad23374ff333ecce729bee6a3a8a9cfb3bec |
|
MD5 | 9ac052b62ee84ae5a122a5eae1e2d2b6 |
|
BLAKE2b-256 | 9f3ea471c252f7609cdc79fb8b99952736504a904ba1a6cea92fae482c589bdc |