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.3-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a848368bdae3a196e1fa5d2b90eede08ab344d1ea1e346022248f2cf33bf9cc4 |
|
MD5 | d0530f1219e550dab14249a07c56dffc |
|
BLAKE2b-256 | c09bb412a9b01ed500d29f35baaffe1780d25055cb505b3b74ad5a480a53edfc |
Hashes for threaded-2.0.3-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 997205677f2b689b028da17dc9643981019969191d1c392fb58849e1dc8f5a2c |
|
MD5 | d66d7dc5df673fd06e84702312134989 |
|
BLAKE2b-256 | e1ecc621434f70c72ead43d81e0fc81a99b1e264b8cac67be4dc2dce058f7da7 |
Hashes for threaded-2.0.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cc6964b7ada5bf6407cbf8d7f0cd8709edae4faebe8dd05704ef686b932edfe |
|
MD5 | 9f5f34be3dc1b4fd34fad82fd6bcfad7 |
|
BLAKE2b-256 | 15ae3310d3721203c8cb1b7683324826c8f7ed984a5639d330da75fe37968d12 |
Hashes for threaded-2.0.3-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b997c9f92773408adb40c88f31a7f7d73b59c728fd4d1a8835d8dd022b5b428f |
|
MD5 | df4aa71ecfaa2910d6f9fbe1ccc55446 |
|
BLAKE2b-256 | 8837c7e5e43784f5c6e6b05539c1be04030d469efd138addb899e5b24af92b9a |
Hashes for threaded-2.0.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3037517a37c8855f22d4387220e0047f1ccafb3d0584f53c65996466af357657 |
|
MD5 | ad7cc86b8417e5ffdb259280312e3255 |
|
BLAKE2b-256 | 380dc231a770807aea1fe9d7f6735c5eaf9a2141d3ea11e85699b09b8144331f |
Hashes for threaded-2.0.3-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b172a03b0a38b3d655ac627ca5dfe79198e151f8d3680e294379e84f0c61bed6 |
|
MD5 | 9aef730152c9aafb7700968bba903a12 |
|
BLAKE2b-256 | b0252260e59836f0a8283b37c9463de590267daad5d8ca672907f9516496e974 |
Hashes for threaded-2.0.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0df2a24ba1794aeb32ffdb16f4bfbeaee319f91d5e28946d6aa63c0b9fd4aa5e |
|
MD5 | 8e57648cf49d50a964a915959397067f |
|
BLAKE2b-256 | e028b3a093e21904f6e884edcb53815148c13a7d873361450b80e1b6fe7c1836 |
Hashes for threaded-2.0.3-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca1d0d03c94f79b7e9dafc9911430f6490ad72f6fcdc8bad6772f8d569a8bd32 |
|
MD5 | d3dadc3389fb342e56ba357214502629 |
|
BLAKE2b-256 | 1cff1fa6dbe974dd070be15de36e7712aad1a18baff01ffce99bddb72a95b114 |