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.
AppVeyor: is used for checking windows compatibility.
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.9.1-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 621499cb0e25bc78ac597e737253f94325addda859e69175fe86aebb3a42f49f |
|
MD5 | 1224fe689429b7a75694ddc64da5d2be |
|
BLAKE2b-256 | 424057e042d8359849c2da8c8f5976d73a8366764be4c6c100742475ee6c890c |
Hashes for threaded-0.9.1-cp36-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b037974d00322c8cf3a2f62ea8af826c4e2673462ffe95f77b2e57376d5780f |
|
MD5 | b4621b5e9e276964fd7027be2fd27176 |
|
BLAKE2b-256 | 938ebbbc84456e9b845c5c9d63d5bacd01272d817f0bae485c8b1cd3fc7bf37b |
Hashes for threaded-0.9.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cb684cc31ecf3d95c7cee3bfa00b4e8b9b29f2324efc4cc516eb58548ccd037 |
|
MD5 | ef4bb83962320ff742ad60c94179d681 |
|
BLAKE2b-256 | bd75c6fd0f183d5ceada96c7e02fe9af45be1b093e1a5ecaf36fadfe365b3d1c |
Hashes for threaded-0.9.1-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2cb47376a19fdaf4ab565567fbf144e173e35bbd85ac064875f37a39a343658 |
|
MD5 | 3c4087b04ba0cf5bbc769e682c25ca86 |
|
BLAKE2b-256 | 9f85be339c722a7fe8475c95c4f4137ea50415adbd13be52448a544eed4ef98d |
Hashes for threaded-0.9.1-cp35-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f88be6757ec33f74a69246d2f6ef247fef4479fdaa33c68d25adc12e9509f53 |
|
MD5 | f926bbed4901771b0abfbcb55b46fd3c |
|
BLAKE2b-256 | b87482752f107ca69400466d24f26e297b3e9efbf35da4b815302b79b864fee0 |
Hashes for threaded-0.9.1-cp35-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b02dec630bdc29164e78e7e8ac91c801f373e83e88c3f9bbadc5540ba5ed3d14 |
|
MD5 | 7b1c6a93570715b0da25e579054d1f4b |
|
BLAKE2b-256 | b27cfcb4f7379f373f82522102bfd0f571ebad42b60ff9dd16db9fcfaa795f57 |
Hashes for threaded-0.9.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | afa629c511d6dbfcee12da708f176ddf8156edaffe53230165dbed629da263b2 |
|
MD5 | 3495608842514c0c22a7e88d8e290f9e |
|
BLAKE2b-256 | 6c06bb331fabdbc20ed04eb168d2c16c05631d53c93165624df697f73f0b2d02 |
Hashes for threaded-0.9.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a20454a66fbfdfdc1461f2f8b3ed0cd814dad3531619021ceb57a95f5dc19cf |
|
MD5 | f22500fd3028177e4590fb5b0d3c34fb |
|
BLAKE2b-256 | 99e3689e349f6fb937c3eb69aec11028d542a3a0ef19486da031ee93d1021d38 |
Hashes for threaded-0.9.1-cp34-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a16f531cf018d76b956b0709779304af104a4c775154a4e7bc30b636d6be42b4 |
|
MD5 | 5e769421987493684b5c90f56aec8245 |
|
BLAKE2b-256 | 76b39e413db6fb71267c8f165d05a63cb5b30a382ce9fd83060fdb10aef63ccf |
Hashes for threaded-0.9.1-cp34-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f38994acc5c7879cd9c11f2595bbdb3c91504a163024d110ed1e4a50c88c327b |
|
MD5 | 58c26a3071262bb13c71c517119da695 |
|
BLAKE2b-256 | 333692849d346ec257d7d89557cf37c2564f523705edae1ec685969cf028971a |
Hashes for threaded-0.9.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db86e22a152adb9377ca46a74b2354630a9361b5f80a51718542139d619a1af1 |
|
MD5 | eba1adc5fe4b8f6f2a2cfa0b3f9f786a |
|
BLAKE2b-256 | a8f28f1635548ce14fc9855c0876025ba6a7ae3b3816c4e0005269eaf141aabd |
Hashes for threaded-0.9.1-cp34-cp34m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e32c673462c140c8e3ecc4b5ee4db6ba700b340c1ce2b6a10982968dab6255a1 |
|
MD5 | 9d9a0f62eab8afab9ec26c48f9e204c0 |
|
BLAKE2b-256 | 64e0fc957c9a45f0299dda3f6a8f2169183f87db9d2b85ab7bb649e4068e4edd |