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/penguinolog/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 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. Test environments available:
pep8 py27 py34 py35 py36 pypy pypy3 pylint
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-0.8.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43195413cd1328c686c3b19fdf19201088f3413954e495cef9e5dad4933a2661 |
|
MD5 | 374f3b343967c0e98e10f41b0d77fed4 |
|
BLAKE2b-256 | 7a5d2f6ecc18cdedd4ea56f91a63f004aa33db8395e8475da0cdb702a98e5a61 |
Hashes for threaded-0.8.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9be373e51306bfbc1e2d8ebd81f5f8554b53ede3e18a8d52ac93e12abb2919a |
|
MD5 | b65d1615cdb49b20ccb048d9c8a78d30 |
|
BLAKE2b-256 | cf3cfcc83ca55ada940f5a7ee9df9557a9ec6f4ecbea43fc521edefb469d97d9 |
Hashes for threaded-0.8.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3c4aee59d6e049115a978be1ffd21e8bac4aa336ab23a9b6fec431e87ac16ac |
|
MD5 | 52f08c6c41db404baf00e3062b80c095 |
|
BLAKE2b-256 | be467339968b1773895b86d59a8efa7d7122719fdd8f8d9e3797c99709769565 |
Hashes for threaded-0.8.0-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80288c730e656bdfa53f2798fd56ecd34c40a766ed7565ee3d061099312de7ab |
|
MD5 | 2b101caa83f00bf8e016fc84efb9a58b |
|
BLAKE2b-256 | c21a2518a9447ac029ed827c2e9d42c48b77eaaafac4afb1d9547116febe87f6 |
Hashes for threaded-0.8.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b740ea857ed8a3c83ded1b5b1f5a3e7a6c75abc31eb426e2dd9cb3fada9ebf53 |
|
MD5 | 40a576812a7d1431eced94cf5c5a9d41 |
|
BLAKE2b-256 | 19129083312741f91400a8e4bdf629a5d6529f7d73108b4c63cb335f72c0a155 |
Hashes for threaded-0.8.0-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c3caebc18a81b02950a96ea16d7eb2b19529ebde7a1d61344b207e91e81c7b3 |
|
MD5 | c5c277983e56bab1abe34750cdca3a38 |
|
BLAKE2b-256 | a81d1cb3ee376ac7154fe03143b91f0a40e9aaa41ff682ebccc048b73d6feba2 |