Skip to main content

Decorators for running functions in Thread/ThreadPool/IOLoop

Project description

threaded

https://travis-ci.com/python-useful-helpers/threaded.svg?branch=master Azure DevOps builds 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.

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.

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:

  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.

  3. Azure CI: is used for functional tests on Windows.

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

Uploaded Source

Built Distributions

threaded-4.0.4-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

threaded-4.0.4-cp37-cp37m-manylinux1_x86_64.whl (685.6 kB view details)

Uploaded CPython 3.7m

threaded-4.0.4-cp37-cp37m-manylinux1_i686.whl (634.6 kB view details)

Uploaded CPython 3.7m

threaded-4.0.4-cp36-cp36m-manylinux1_x86_64.whl (685.0 kB view details)

Uploaded CPython 3.6m

threaded-4.0.4-cp36-cp36m-manylinux1_i686.whl (632.9 kB view details)

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: threaded-4.0.4.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.4.tar.gz
Algorithm Hash digest
SHA256 b40d609f1b4ef4aba262b08cbf2a79a82e8f55684e1b47926ac6456f781c223b
MD5 f6ed92f2df0844ac1bf58d6680dd79f6
BLAKE2b-256 7f775a25040e53dcfa632d6f0ffbe94059822bb5327ef77cd2d9f44bbc6db4bb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.4-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cb8c09184284abdc108b416991da8bab53c2a381f46d73564d1060ec1e992463
MD5 fac527ccc076a94143fa220c0990d084
BLAKE2b-256 2e017d0f20dba03b17ce79715a551d67b817843e8e1c384dd979e847de16f64c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.4-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 685.6 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.4-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 647269e3a1df9f91aa0f0f3f1a5558b0e3d4b33280fc6f41718c9a0a12cfb190
MD5 fd95d0086ece27e172a229e8cf04a9dc
BLAKE2b-256 9f7fae69a1c938f9eb347361a3191ba16e6d9aadfea160f1e64c2a94a13fede2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.4-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 634.6 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.4-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 27a8275a9c803baad347d78c020000b20f1c9a4114604017877eacda190fc9f6
MD5 7cafdd8422bab131e01c12a9b84e928d
BLAKE2b-256 5a8e16601b72b17dd0919556a8495828cb90674460133a4b5b3b94bfafa451dd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.4-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 685.0 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6de8d8c42dfb388ffd400759dc36e1dbada79ecaec9cd7c0a06cf3d40032b8ee
MD5 7bb2843c60b2960e08a9cbc5e1178ec9
BLAKE2b-256 d12defa34a80aa951ebd0eb7f29011f9552bd60a0c358954cc876e28d7b05e3f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.4-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 632.9 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.4-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 1a294af46c502fb981974be5a42cde5b3d3b6dca349ce5382df1b2b190ed033d
MD5 93ffdde7200700d7599d178ef15dfff3
BLAKE2b-256 cd41a8d1b4872131961bd9bd8ea1598aa301addca8557326e99cf86d61eb6b63

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