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.5-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d237ad3a22be4c075de9aa947d513f1c3980c8c0444cd265ddf874a841dba1fc |
|
MD5 | 4f4d0f64007cfb9aea9c3f823941a295 |
|
BLAKE2b-256 | 3217eb86faaed01fbf9f1032c1829278512c98902a73e2d7743431631b2fece0 |
Hashes for threaded-1.0.5-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ba01464abd076f553edecdb32743c00cd6f2ae4eb6f27242ec774a2c26d24d7 |
|
MD5 | 423ca019f61dd33b48477cd7385a343b |
|
BLAKE2b-256 | 69cce59cd9b544f772cb5d9d5ddcc71ad9ae9a41a19a2aa4cd1d9648984cc609 |
Hashes for threaded-1.0.5-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c90b1c736692757cb75ea5ea97abee0903c9f1e523942174d36d4c321860369a |
|
MD5 | c22f295422f4e19c8eeb27c9f093defc |
|
BLAKE2b-256 | c32e2f9c8980ed4969c21f80c4ac5723634e9affff8510381fa90f016d6bac81 |
Hashes for threaded-1.0.5-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2c783fa09af41f46c9a853845c0ee9e450277d1ed098508accceb3bed28c2ac |
|
MD5 | bc77c93a623f7f2618943b88a5e33eff |
|
BLAKE2b-256 | 089a76dbffe5dfbf6ad1b122ee80565d7e2ff3a9e528f64216080955fc6db5fe |
Hashes for threaded-1.0.5-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ad5ed526621ac63f3abca9fbcf633423564d223391f4faef47e5f607e98f956 |
|
MD5 | 1df282b2a5dbf421949579ee6458f7f6 |
|
BLAKE2b-256 | dc5400896dcc9181ed3dded4241005736692af51ae556570c5cbc93e6e7b1f41 |
Hashes for threaded-1.0.5-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 614ed64f1805d99f541d5730883f9aef51048ad0528d00254c57ac39ee52d49c |
|
MD5 | a79941a0cf2b84d6e382f12d6242d8b7 |
|
BLAKE2b-256 | a43a05778bb636df1c483571862495731d516328ff57803109e62d13779c04f7 |
Hashes for threaded-1.0.5-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 391bdb10497ec7309c1f53fedc50dd0dbe846b3abed0151d861c55d9baf80a14 |
|
MD5 | 9cd029bfc8da1dae2115b5413da83b55 |
|
BLAKE2b-256 | a9faf2869b7cff5be5818ae8cc7c9f6827217f246b3ddc631ece102d6256b88f |
Hashes for threaded-1.0.5-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77cb2b47e9385f766ac586099217c70c2b18a73ac4a0659884fc39b44ef510c3 |
|
MD5 | 7d8dbe836c5131276d55a582290c4007 |
|
BLAKE2b-256 | 4d622220ca15b1b40c836205289193411d08a7462873185a3998a77d76bc3e8c |