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 2.7 Python 3.4 Python 3.5 Python 3.6 Python 3.7 PyPy PyPy3 3.5+ Jyton 2.7
Decorators:
ThreadPooled - native concurrent.futures.ThreadPool usage on Python 3 and it’s backport on Python 2.7.
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 Python 3 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)
Python 2.7 usage:
@threaded.ThreadPooled
def func():
pass
concurrent.futures.wait([func()])
Python 3.3+ usage:
@threaded.ThreadPooled
def func():
pass
concurrent.futures.wait([func()])
Python 3.3+ 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.3+ 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-1.0.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9861085b0e5219d3da1d7243100f3cebffbb09012a1a2ad639ec79f46f0acd2b |
|
MD5 | 656c449fc18e11b9f168d7afb5bbe18e |
|
BLAKE2b-256 | c2eacb3c73d9a7660f93399e26b58ca3d011a9ffe84a66ce47a443bbc948ac1a |
Hashes for threaded-1.0.6-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e9398d3f9e1b4b90477ad25eea467bc2251625a44efc703a37bfbe877a9680b |
|
MD5 | bc11decd233dc683ad9f792c23f40cdf |
|
BLAKE2b-256 | 48603928b7c76b69a73d92ace1ba2d341049466f2ba1336731cd12ea99e4f463 |
Hashes for threaded-1.0.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2825de5daaf774a3f91e899b01a79e1680c453889194cacc5c86d4cfa16c9e1c |
|
MD5 | 1859f373392f4d5bd8cba27844099787 |
|
BLAKE2b-256 | b4f550ae9fd3a119b512a9345ee9d80ab8bf7c1a5a37275c33cdf207284f6d38 |
Hashes for threaded-1.0.6-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be189dfe38b86b271f12b2e43cd151a24974251f33e52968d23439976ce26658 |
|
MD5 | 526e4048d3da32147f4ec1f40a202920 |
|
BLAKE2b-256 | 0e4b641b412a30a90889baea970411d387637c10a10e52ffe0c9ad5315b4b4a4 |
Hashes for threaded-1.0.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 120f87e75b44f97e3b69658f7b8ce1b691c6ccb204acedca8a7dba1563fb6b6a |
|
MD5 | 0c8490b736955bc8558e5d3d98fb3c07 |
|
BLAKE2b-256 | f3c6195089e0b4b4a7ebd37bbb75ae46862793a8ac003dcbf4d15460701ab3db |
Hashes for threaded-1.0.6-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b44c8caaf07ebe08186f6f64cc76b3bd23a6b197ef515dd6f0ce9793daa63d17 |
|
MD5 | 457faaf0613c1cfd0003f67938b67ff4 |
|
BLAKE2b-256 | d5b024bc11d3ee97702d7fb7a26cf1e1f829624aa080ed1ca57f461db96963a2 |
Hashes for threaded-1.0.6-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c73e9e45157c97d87fef5dd6b0d630e3c444d8b5c77b5332e3edcddd59b7624 |
|
MD5 | cb3cf2aab7660b72add6321982800265 |
|
BLAKE2b-256 | da4e9ff69e984f05fbb7064061c67dc56adebe027c6c30a88f0630728acafa18 |
Hashes for threaded-1.0.6-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3ca1a218fa18a97b4ab3c35c16fe9e23ee0819054ca94678332e86bdb6186c0 |
|
MD5 | fa2920e288fb7bc72b495c888f38360f |
|
BLAKE2b-256 | ee3d1669cfcc56f4c128ef1511c5e7a0d2ece9c7182f51c36041db592bdba3c5 |