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.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95e0d94b7ca82bc48072fafedf7b2e63822a28f71608571facfa4b585dcdbbcc |
|
MD5 | d952cf22530c04f500786bd80e411201 |
|
BLAKE2b-256 | 830eb5a52badf65fd58aa2cc8d990697fd4603f326038f95639dedd0a7289954 |
Hashes for threaded-4.0.1-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53fb5085b31167a1c633c87b05628b1d329de7e7d5c3aa67d46f67680f2ebb44 |
|
MD5 | 05086af34ea4b8190001a0db38952cdb |
|
BLAKE2b-256 | f77b23b3a6d73e9ddf707828eb5f856752283c951bc25d8e421c973f35e4d146 |
Hashes for threaded-4.0.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15b5fe4fe40ccc90cc31f6d502ba9c1ab1c479bdc49c122b690845e1a78fd118 |
|
MD5 | 74d4af54d482986771ef15c8f61a1eba |
|
BLAKE2b-256 | 69d5c3cf13a3c3545dfd67a84afaf70c1b07b07a8840dd3e95ebf99d0b9e0286 |
Hashes for threaded-4.0.1-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d00fd390b825764cb7df14c99d26b6530c633aaa64421c632f5f4fa94b2d91d |
|
MD5 | e14a86552de2f6a54db697361251c18d |
|
BLAKE2b-256 | c9e613b2f68d436b3cc46972f00ace5018e6538bb17317494af984bc10e3cc0f |
Hashes for threaded-4.0.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbdbb8ee8a5fa600d58d14895156ccfb362cd0ea2e3da2ec650511b212b77a69 |
|
MD5 | ac59c70f4cceb1bf4a117d3c2704ad50 |
|
BLAKE2b-256 | 0ffe216e53dd6522c98581e4e28983c98f0f33791c73ae2e32bad2b81389d538 |
Hashes for threaded-4.0.1-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22200d12abebe5d6fedb5bd941f25d47e8269a899c636a497fa37f925312f9e7 |
|
MD5 | 20639a2599bfb693c2b4587e3a94fd71 |
|
BLAKE2b-256 | 22d593e2183cdbb5dc3728e79bb5e81061c1a9df03ac35cbf57cbf6188dc74f7 |
Hashes for threaded-4.0.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14a8647bc8ee16112225109a63b997d95bd3874d7d0c013c4e79341a215fc965 |
|
MD5 | 53bc8d6ee27638ad97304061a5518071 |
|
BLAKE2b-256 | 5c2c398362c66325cd2baac7feb5ec1d34ff8fdad43cd253d1949fc49f9be33f |
Hashes for threaded-4.0.1-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ce8403ae2ca69e789481a485ba807f6e5bb79129e5b6c56ea07b58f697777b0 |
|
MD5 | 1ebd1839001e5ffaa404c559bbe36c5c |
|
BLAKE2b-256 | 5c9beee304cfdae88d801907677bf863910aa287a51fd4c841deda236a00538e |