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 https://img.shields.io/badge/code%20style-black-000000.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 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.

  • 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)
@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))

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-2.0.3.tar.gz (17.4 kB view details)

Uploaded Source

Built Distributions

threaded-2.0.3-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

threaded-2.0.3-cp37-cp37m-manylinux1_x86_64.whl (722.1 kB view details)

Uploaded CPython 3.7m

threaded-2.0.3-cp37-cp37m-manylinux1_i686.whl (725.9 kB view details)

Uploaded CPython 3.7m

threaded-2.0.3-cp36-cp36m-manylinux1_x86_64.whl (721.9 kB view details)

Uploaded CPython 3.6m

threaded-2.0.3-cp36-cp36m-manylinux1_i686.whl (724.8 kB view details)

Uploaded CPython 3.6m

threaded-2.0.3-cp35-cp35m-manylinux1_x86_64.whl (704.8 kB view details)

Uploaded CPython 3.5m

threaded-2.0.3-cp35-cp35m-manylinux1_i686.whl (707.9 kB view details)

Uploaded CPython 3.5m

threaded-2.0.3-cp34-cp34m-manylinux1_x86_64.whl (720.3 kB view details)

Uploaded CPython 3.4m

threaded-2.0.3-cp34-cp34m-manylinux1_i686.whl (720.5 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-2.0.3.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3.tar.gz
Algorithm Hash digest
SHA256 0a2ce3f6fb0a3944bfe25e6430c060e4aab9742b827a9ad5586ef03ffeb41b04
MD5 ffffe71e4dafa58607d16ddbf5ac5ed7
BLAKE2b-256 24f889daaab92dd2790ca2b8f0796566dc611a3f3bef62eb9698747baaf1393d

See more details on using hashes here.

File details

Details for the file threaded-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: threaded-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fd2d05d798f66e0ff5ff75ad993e77bf0d32c84056d5e773ba3370a3162e9eb8
MD5 8129214fc97ecfa37ffbd97c8ed96b76
BLAKE2b-256 3dfd3ff2ab65468e185a75f28907e33860b56ea72508e5e021530037010bb351

See more details on using hashes here.

File details

Details for the file threaded-2.0.3-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: threaded-2.0.3-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 722.1 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a848368bdae3a196e1fa5d2b90eede08ab344d1ea1e346022248f2cf33bf9cc4
MD5 d0530f1219e550dab14249a07c56dffc
BLAKE2b-256 c09bb412a9b01ed500d29f35baaffe1780d25055cb505b3b74ad5a480a53edfc

See more details on using hashes here.

File details

Details for the file threaded-2.0.3-cp37-cp37m-manylinux1_i686.whl.

File metadata

  • Download URL: threaded-2.0.3-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 725.9 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 997205677f2b689b028da17dc9643981019969191d1c392fb58849e1dc8f5a2c
MD5 d66d7dc5df673fd06e84702312134989
BLAKE2b-256 e1ecc621434f70c72ead43d81e0fc81a99b1e264b8cac67be4dc2dce058f7da7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.3-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 721.9 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7cc6964b7ada5bf6407cbf8d7f0cd8709edae4faebe8dd05704ef686b932edfe
MD5 9f5f34be3dc1b4fd34fad82fd6bcfad7
BLAKE2b-256 15ae3310d3721203c8cb1b7683324826c8f7ed984a5639d330da75fe37968d12

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.3-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 724.8 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b997c9f92773408adb40c88f31a7f7d73b59c728fd4d1a8835d8dd022b5b428f
MD5 df4aa71ecfaa2910d6f9fbe1ccc55446
BLAKE2b-256 8837c7e5e43784f5c6e6b05539c1be04030d469efd138addb899e5b24af92b9a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.3-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 704.8 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3037517a37c8855f22d4387220e0047f1ccafb3d0584f53c65996466af357657
MD5 ad7cc86b8417e5ffdb259280312e3255
BLAKE2b-256 380dc231a770807aea1fe9d7f6735c5eaf9a2141d3ea11e85699b09b8144331f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.3-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 707.9 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b172a03b0a38b3d655ac627ca5dfe79198e151f8d3680e294379e84f0c61bed6
MD5 9aef730152c9aafb7700968bba903a12
BLAKE2b-256 b0252260e59836f0a8283b37c9463de590267daad5d8ca672907f9516496e974

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.3-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 720.3 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0df2a24ba1794aeb32ffdb16f4bfbeaee319f91d5e28946d6aa63c0b9fd4aa5e
MD5 8e57648cf49d50a964a915959397067f
BLAKE2b-256 e028b3a093e21904f6e884edcb53815148c13a7d873361450b80e1b6fe7c1836

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.3-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 720.5 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.3-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ca1d0d03c94f79b7e9dafc9911430f6490ad72f6fcdc8bad6772f8d569a8bd32
MD5 d3dadc3389fb342e56ba357214502629
BLAKE2b-256 1cff1fa6dbe974dd070be15de36e7712aad1a18baff01ffce99bddb72a95b114

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