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.
GThreadPooled - wrap function in gevent.threadpool.ThreadPool.
gthreadpooled is alias for GThreadPooled.
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))
GThreadPooled
Post function to gevent.threadpool.ThreadPool.
threaded.GThreadPooled.configure(max_workers=3)
Basic usage example:
@threaded.GThreadPooled
def func():
pass
func().wait()
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.
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-2.0.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c13d7ff8b8afa8f4604b42b4e95c0ca9fa3e13e1440410530b4fe7055fd3af2 |
|
MD5 | 88f767fd2adfac4f5d99628fa86aa036 |
|
BLAKE2b-256 | 232d09482ab320efb4575c72596600379cd7fc8a0ae9b9b3da898504ddc1153e |
Hashes for threaded-2.0.2-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a06823effce2688e47ce364a0217463a20d3497fb5f004491ee7cd8f6c38a0a |
|
MD5 | 9945966ddd224787953e43c480ccba77 |
|
BLAKE2b-256 | 95f71a2ca3049377f7218c51689a30e59bd1535f16ba496ec987f99e53b6f0a8 |
Hashes for threaded-2.0.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79b2c6cf83ba4e9a1b613c50d5b6cccfd35d149f87d9f3916cb65491ca33684e |
|
MD5 | ec5370b58d2359895477a9fa4a0b3b91 |
|
BLAKE2b-256 | 535053b50faad361760fa678138c7a547f42488135ef9eae5665af5835cba897 |
Hashes for threaded-2.0.2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad8b416f06e5f896d78cf16bb27620f2cf68b3e47aab9513c4535eb7e7d5efb9 |
|
MD5 | e374d13c936b0f2d8ea6725a477495f0 |
|
BLAKE2b-256 | c6dabbb189f9b4b7b7dc385cc891c46847e529f4bc003141382604de905b92f7 |
Hashes for threaded-2.0.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be184c144d7d2ace8404054ebd0f6ba7396db53f3ecb44341a5f35a8c299cef2 |
|
MD5 | 4b0953a86e0bcf55cf60efedeeb84606 |
|
BLAKE2b-256 | a96d5eef992f067120c09fcc2204f1f9aeab626425a23442357e1de72883aaab |
Hashes for threaded-2.0.2-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a87735270dd99ab3607fc480c7677b68f56707d9ae556f8a16ecbc007766e776 |
|
MD5 | 2730e8473353124d3119875a0f8c9f97 |
|
BLAKE2b-256 | 0373dccfe8c2f9d440da42d25816ab520e0bc6ab3a7105aef2f402ae3ae4b1f2 |
Hashes for threaded-2.0.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36a2aabd5a1d5e8c5c0abf82bdb12a91d53f5822a341c4c86c59cd2954178058 |
|
MD5 | 5f0512dad1910365c846ae0c3ca4097b |
|
BLAKE2b-256 | 2bf3de1eb7a3d7ad67f9a82d3011c5355943c4bfc7c0af064ccb3857fa5ede6f |
Hashes for threaded-2.0.2-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 194f6831132f05640bfb23276e5cd882e46f4579292cd0ff9601492760826ea8 |
|
MD5 | 1d6d873d308c409d394f33d01c078e7c |
|
BLAKE2b-256 | f74cd4c5d1dbb2e169c52b7464102bde0c465d46f3bb0710fb832f0a3cb067aa |