Skip to main content

Decorators for running functions in Thread/ThreadPool/IOLoop

Project description

threaded

https://travis-ci.org/python-useful-helpers/threaded.svg?branch=master https://coveralls.io/repos/github/python-useful-helpers/threaded/badge.svg?branch=master Documentation Status https://img.shields.io/pypi/v/threaded.svg https://img.shields.io/pypi/pyversions/threaded.svg https://img.shields.io/pypi/status/threaded.svg https://img.shields.io/github/license/python-useful-helpers/threaded.svg

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:

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:

  1. Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.

  2. coveralls: is used for coverage display.

CD system

Travis CI: is used for package delivery on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

threaded-1.0.3.tar.gz (294.3 kB view details)

Uploaded Source

Built Distributions

threaded-1.0.3-cp36-cp36m-manylinux1_x86_64.whl (736.8 kB view details)

Uploaded CPython 3.6m

threaded-1.0.3-cp36-cp36m-manylinux1_i686.whl (699.5 kB view details)

Uploaded CPython 3.6m

threaded-1.0.3-cp35-cp35m-manylinux1_x86_64.whl (718.5 kB view details)

Uploaded CPython 3.5m

threaded-1.0.3-cp35-cp35m-manylinux1_i686.whl (682.8 kB view details)

Uploaded CPython 3.5m

threaded-1.0.3-cp34-cp34m-manylinux1_x86_64.whl (734.2 kB view details)

Uploaded CPython 3.4m

threaded-1.0.3-cp34-cp34m-manylinux1_i686.whl (696.2 kB view details)

Uploaded CPython 3.4m

File details

Details for the file threaded-1.0.3.tar.gz.

File metadata

  • Download URL: threaded-1.0.3.tar.gz
  • Upload date:
  • Size: 294.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for threaded-1.0.3.tar.gz
Algorithm Hash digest
SHA256 b6817c80d186c3984370885597d109c59ad6708b26370f4b04b7ca2b39292660
MD5 b6a98a1ad8384f6d2bcb71d4ecc43a1d
BLAKE2b-256 7b95f375ec9fa95a5e30ed7440c86c411e03c56d9e4672c8be7bead3cb2191ec

See more details on using hashes here.

File details

Details for the file threaded-1.0.3-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for threaded-1.0.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f4af5113bb2019ea083bf3cffe7d6af5fddeb7552cfd497b0cdb74507721beea
MD5 2f908b87691cf333e390519907d32161
BLAKE2b-256 a6634ebc4f48af186f240eafdcefd88864c8d7718b7c931d71aa34f16174e0d6

See more details on using hashes here.

File details

Details for the file threaded-1.0.3-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for threaded-1.0.3-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8871ff52c6e9eddd6709006d275c1875d0211fa2ac4f3e96d1466bea0ede1654
MD5 a5de3ad644ca7e87a8c57e91f48d9635
BLAKE2b-256 39a377ab070cfc581f349e32f0b98fbbbae5bd5ff399ed2e7db9f185bf6e21ef

See more details on using hashes here.

File details

Details for the file threaded-1.0.3-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for threaded-1.0.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 98898055c74c38d2966f9f9c2023cf8de9e69672bd97706a87022c62ae3a3469
MD5 2cd80fde5ed0720f287d03649095efb8
BLAKE2b-256 2cc82e4085734b4afd036067f25e4c3e67da59f2458f903058bcd5554478345e

See more details on using hashes here.

File details

Details for the file threaded-1.0.3-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for threaded-1.0.3-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a468b3fba0dfb940d30655c6d34a9df7d041c1e94fd34b610c3ae6009178cb52
MD5 75d744601beeadb7f07884a1dff50607
BLAKE2b-256 96bbef3223ecda49a2c2ceb28cfc46f4705c2a662159b8d9524c58b71f4744b2

See more details on using hashes here.

File details

Details for the file threaded-1.0.3-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for threaded-1.0.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 667e20c576f51b6f272cad31406bb41f48544937ec8e142a428655bc78fe2b87
MD5 0096db09a31f75c9ddab1bfc189b0212
BLAKE2b-256 1f5198fe165190f0aeb195e8dd857aef005952ed967117094294881c832b98dd

See more details on using hashes here.

File details

Details for the file threaded-1.0.3-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for threaded-1.0.3-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ea81d38d5fbc5be2152fb9288154dfd123f6a54c542c1667527a882b6446c42d
MD5 a580aab89ddf45bd4a0537078a6361c6
BLAKE2b-256 44a0542685e0a7a782af57d2c134686b4eda93372c26b1de94b20c39e5630bd9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page