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.
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))
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.
Azure CI: is used for functional tests on Windows.
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-4.0.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e03570fa7f24842a80a59185c864935f0068aa7ca82160459e6b00d719cb3ed |
|
MD5 | 75e27f764fff2a3a50c5aaf67a5c680f |
|
BLAKE2b-256 | 706122a20799cf25e9feeda23b1ef09908a7f2b5fdf0b0393277437545ab1e9b |
Hashes for threaded-4.0.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 026615f2c4f315841ea4066b5a75ef52aeb47c7c761f55f5955069d87100a629 |
|
MD5 | bedb336b9d3a770d3b13b86bd01698cb |
|
BLAKE2b-256 | 689c1d4c074b7a0634c5449c1fe636a64477ff50f58de35044bf2fb4960b326d |
Hashes for threaded-4.0.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3457304bfec596b4f0d24bb37d999f107d9d0bf0956f46c2fc770b7d60d12035 |
|
MD5 | ec66357b620aafe00cc2eb4f006056c1 |
|
BLAKE2b-256 | f856b252bc0d0b39b2d2449e8eb0a172193f7df436180a7fe6c77c0bfa622cd8 |
Hashes for threaded-4.0.0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52f9b229f074bbf278c80647c10180e0d5ffde9470d86e5f078e8e86d34648f6 |
|
MD5 | b91a565c4b8788c59c8f5f52d867cd62 |
|
BLAKE2b-256 | 57fc75112ed540bf5ef9a0f17d75ab2d8ab0b395de17f43ea723fad5ca2a978c |
Hashes for threaded-4.0.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e6b39d4e0cee275f9dcfb928c221d50b026e7660e6ec7718555a5f11cba9e3f |
|
MD5 | 893eab0872b4b5132e8833bd4129eb43 |
|
BLAKE2b-256 | 861c88cc7761b6810fa062a0cedb55d7b674475463616d2abfe99a28400e4af3 |
Hashes for threaded-4.0.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19e4101c04783d6aff4d43f1b0f893fe4e72a4e428a2c9a3705c5b921fe3c7b5 |
|
MD5 | ef6e7dbb3b6be9e99c1e49d91442a264 |
|
BLAKE2b-256 | 566e3366b6c2dbf9f81ad4c357678e1eac699fd1c91feb1b33f4003fa6b2f41d |
Hashes for threaded-4.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1cd34461945890a97514b8e55335102014e0e0499e6cc43d61cfff822f7e79e |
|
MD5 | 34fce7debdb0f54d1565c01ee48eefd7 |
|
BLAKE2b-256 | 612213f4a906c643182568dc06db94c046a9912c1cae9273df72048f21c1e263 |
Hashes for threaded-4.0.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 659649e7cec6b37872b2b6c95fb852262fc497d384e5598a2fbe6bfa17e78e85 |
|
MD5 | a381ed267d0da9a1ab2ed8736944f1de |
|
BLAKE2b-256 | 362c38d4a206088d6d9a9557a384e026a3e0b821791528d82decb229f85d0f15 |