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.post0-cp39-cp39m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cac9af372f60ccf417085d7035ae7b89a6b9ab23ed94460efc174592f14669b9 |
|
MD5 | 004b3f1513ececfe0bcab27c2fd4f069 |
|
BLAKE2b-256 | f4c4631ce66545e8ceeefbeb48e0426e35de42c0d5637b102dd70ec56add2911 |
Hashes for threaded-4.0.9.post0-cp39-cp39m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26e4fad757c31ce9fe5d68e1ce6165596b2764a3817ecd4926a470f3289ad83b |
|
MD5 | 1243a5d3068bd615244f572f5541c5cc |
|
BLAKE2b-256 | c17d49b4d34973943ad745c8d28449551b8133492c9ff173880a30d35393ef13 |
Hashes for threaded-4.0.9.post0-cp38-cp38m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a08e8bc9c309038b8b1b4c9533b017a3305dea01bccd931e2ca92e79f2f3305 |
|
MD5 | 1516decee3e4d631b871eb10811d4c0e |
|
BLAKE2b-256 | 1888d849624093ac26665dc9fa7d385abc585f2cfd79052469d682b7aab583fc |
Hashes for threaded-4.0.9.post0-cp38-cp38m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39cf9ca6c902c5421d249539085c02a48661aa32f834c47ae8622be355ded1c1 |
|
MD5 | f199dd4d7ddd8ccc303ee2dbb1776683 |
|
BLAKE2b-256 | 73450fb4e84e0d87add0c36d9d43060a21626ace038c5ab66e8ac33f0ad93d70 |
Hashes for threaded-4.0.9.post0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84b0ef58471e770eb7db5bd4aa0a10ed811b2a99181bdebd178a5dd14ee72bab |
|
MD5 | e61a1418b531b8fb2655dcefa76ce386 |
|
BLAKE2b-256 | 440ee99a9ea69b40abefede3b1461637e5e461d046f9dfea9f75becebf1e4069 |
Hashes for threaded-4.0.9.post0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c74f9ffdd20a2838cca9230ba4a699812c259d28d99491ad2689507057528b7 |
|
MD5 | 968654d8711d4d295c3bfa20dc560ed8 |
|
BLAKE2b-256 | 328f76fe2dc2641cb0cb2830b9cc330c25d5116f9ec18c146cf85d39ac9e4810 |