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

Uploaded Source

Built Distributions

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

Uploaded Python 3

threaded-4.0.5-cp37-cp37m-win_amd64.whl (189.2 kB view details)

Uploaded CPython 3.7m Windows x86-64

threaded-4.0.5-cp37-cp37m-win32.whl (162.1 kB view details)

Uploaded CPython 3.7m Windows x86

threaded-4.0.5-cp37-cp37m-manylinux1_x86_64.whl (680.3 kB view details)

Uploaded CPython 3.7m

threaded-4.0.5-cp37-cp37m-manylinux1_i686.whl (629.5 kB view details)

Uploaded CPython 3.7m

threaded-4.0.5-cp36-cp36m-win_amd64.whl (189.5 kB view details)

Uploaded CPython 3.6m Windows x86-64

threaded-4.0.5-cp36-cp36m-win32.whl (162.1 kB view details)

Uploaded CPython 3.6m Windows x86

threaded-4.0.5-cp36-cp36m-manylinux1_x86_64.whl (679.6 kB view details)

Uploaded CPython 3.6m

threaded-4.0.5-cp36-cp36m-manylinux1_i686.whl (627.7 kB view details)

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: threaded-4.0.5.tar.gz
  • Upload date:
  • Size: 26.9 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.5.tar.gz
Algorithm Hash digest
SHA256 0d9710a0ce8e42c9d62ba8bcf23eefe930e6df9386475478504971cb9f85fe46
MD5 1ac61c6e29d143bf9eb0e0a7e6184e25
BLAKE2b-256 13e57c05a22bec046e9d3fc6003d5855c1f6c707302971b2b87de9cf6e962774

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3335f10b2b69457d9a3e4e093c5f85b08df0d5e28614a5201705ceaa88b54460
MD5 7f84887c3aeae5827c4952b0b08a3209
BLAKE2b-256 b70bfb3e983ccab25d39f37684a50baa46dcbac75a16217a1915ed8dc184b25a

See more details on using hashes here.

File details

Details for the file threaded-4.0.5-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: threaded-4.0.5-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 189.2 kB
  • Tags: CPython 3.7m, Windows x86-64
  • 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.4

File hashes

Hashes for threaded-4.0.5-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 519492cb4eb43c2d5dabc91ec39e26ed70781269862a32c141ddd57bd2bfdc97
MD5 3381a499ac845d8653e981b54d08fe9a
BLAKE2b-256 8db8402b75acb5cda7efa9cd3713f9e80065e6c0e208a30f99075a7d05496394

See more details on using hashes here.

File details

Details for the file threaded-4.0.5-cp37-cp37m-win32.whl.

File metadata

  • Download URL: threaded-4.0.5-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 162.1 kB
  • Tags: CPython 3.7m, Windows x86
  • 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.4

File hashes

Hashes for threaded-4.0.5-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 313ac1362832f2c7b0019d0f9100ebf00b208ff83c399ff871421c6125a9b048
MD5 b0bb23466d018fe8ac555460d3453148
BLAKE2b-256 06ec29d9afdaca9de1ec6095b7c1203636bf3cf9cca264019723c6d326252b2b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.5-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 680.3 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.5-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 daebb2447036039f8a2500bec83c89c559473e5766195543a9471a4e52f58cfd
MD5 ff224aabf773249f9b555f4879db4063
BLAKE2b-256 c5f3aef10f157d817850e4c6fcf25fb93bbc601a81fbbf5a768a9beef9a1df1a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.5-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 629.5 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.5-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 29ebc5576ccd247b150a8410ecba0a0c93d24109bfdaf9c1de540b4a28b33047
MD5 0f03141949346b8cd90eed93c4316596
BLAKE2b-256 179f2aced5dbc53669a4791207618233dcd3c71c5661eca2ab0e2bcf31841143

See more details on using hashes here.

File details

Details for the file threaded-4.0.5-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: threaded-4.0.5-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 189.5 kB
  • Tags: CPython 3.6m, Windows x86-64
  • 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.6.8

File hashes

Hashes for threaded-4.0.5-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 4b282b0638c0e84b712546c93a82ca12f32e25609f95b2754b279dbe0b8893a6
MD5 ae905ed9799131fbb6916efc7203dce1
BLAKE2b-256 a36327c1ff956cff9a4ac594e9d66bf38ed9c01765d0890959bbe9e8e532454f

See more details on using hashes here.

File details

Details for the file threaded-4.0.5-cp36-cp36m-win32.whl.

File metadata

  • Download URL: threaded-4.0.5-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 162.1 kB
  • Tags: CPython 3.6m, Windows x86
  • 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.6.8

File hashes

Hashes for threaded-4.0.5-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 2a966a7465ea9f7228e42b8b198c265b2f8fb89c6466be499629a590bfb87849
MD5 6958a92e06d205871eb0d32e7f3a9e96
BLAKE2b-256 77bd29df4bf122047d8a89b2db8c6f3fbf50ebdf5dd6a978aa9f83aeefe04dcb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.5-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 679.6 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.5-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 08a6e218beac965324e8e33f233ac1b634e6e18555e14922819465fe01d23d37
MD5 51fc0747406ca003464658792faf92bb
BLAKE2b-256 b9362178c5c1a2594b4786411ea63e74f4ae490beca5add8a04889e467507571

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.5-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 627.7 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.5-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 86f3f265d89ea52f0ea7d529f4b3740c0782db9520e5f5abbba67b4df7113751
MD5 e06daaa70b728b2e6304896b679f5223
BLAKE2b-256 f96cfaafebd5b0abc8f9bb6d392f1c6fb6c347a2eca3d48d9ad1e1150508447c

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