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.
gevent.threadpool.ThreadPool if gevent is installed.
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.2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b9c08646c454b3451f2d0b8e8cd9ec430e6a4382f04cc07ac1a6d094e3b6c64 |
|
MD5 | 2f16d78e1e90f51b34dd9c566f8a7f63 |
|
BLAKE2b-256 | d0cd5035267d72903dbc9fa18f088012bb5a08fd21c7e3bc890665c76f768b5e |
Hashes for threaded-3.0.2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02df59e5da7cdfdf114fe59a842718111821e2187c748ed5d7edca437e83ef03 |
|
MD5 | 4b8d1b5f6b41c69d1f74b6091e48e481 |
|
BLAKE2b-256 | 3c9d98b30fb91b2266d4884ecfce548f98dec603354ce4ad6e4183d844e06fbd |
Hashes for threaded-3.0.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ae977843fb8e3258976a2dedba34f1e84610b95cfa86fcd4c8e35a05b5279c7 |
|
MD5 | 646047e6a8b1e826da2b4185cd659e7a |
|
BLAKE2b-256 | be323e0e6fe5f6e55bbf3ab81161c3b5f33a6e9850c033453e22b749e3408be0 |
Hashes for threaded-3.0.2-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b5b6947b26d8b713cc10630a89be4b185200cbbd1acc64ccc637a24a6dae88e |
|
MD5 | 7af07c96fdf91f27e5264ea737c5d2d9 |
|
BLAKE2b-256 | c01f46dfe21d5d17151452cadc8e0e2a23cf575475ddb9cc3175ed352da88f4f |
Hashes for threaded-3.0.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11d7f7699261fe1a7481d94adb6c68c570f2e93e0fc109b2f3cf91f9708bdabe |
|
MD5 | b1a704afd389c0f28caed495181b40b8 |
|
BLAKE2b-256 | cda48ea4e857650ec2f9e6ca21eaca38ac7f8db1b9d8835462c45ed7269eb578 |
Hashes for threaded-3.0.2-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2c3d7bbbfb5de4df028ce29a5be5a7f693592ac7473025b27f67df1ea5381cd |
|
MD5 | 818237dfa249e9153ba493f7424d5145 |
|
BLAKE2b-256 | af99d56adceb8f8a05a4dbdbcad5d0a7f79b9ca482202d0a2bc54c87d9bbb760 |