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

Uploaded Source

Built Distributions

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

Uploaded Python 3

threaded-4.0.8-cp38-cp38-manylinux1_x86_64.whl (741.6 kB view details)

Uploaded CPython 3.8

threaded-4.0.8-cp38-cp38-manylinux1_i686.whl (681.4 kB view details)

Uploaded CPython 3.8

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

Uploaded CPython 3.7m Windows x86-64

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

Uploaded CPython 3.7m Windows x86

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

Uploaded CPython 3.7m

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

Uploaded CPython 3.7m

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

Uploaded CPython 3.6m Windows x86-64

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

Uploaded CPython 3.6m Windows x86

threaded-4.0.8-cp36-cp36m-manylinux1_x86_64.whl (679.7 kB view details)

Uploaded CPython 3.6m

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

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: threaded-4.0.8.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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8.tar.gz
Algorithm Hash digest
SHA256 3eab570f9270cdbfd1e6fbae3d59f5c0753d8f46c3e8c0ae431319b08d24f748
MD5 dab0ad3932bd6e8aa2178ee4d43be52b
BLAKE2b-256 03f448df89c83a472778cf149640da385ed7145647d2572d2ddba761607c4382

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6ff43d7e5e3adad6ae30344bcd111da49c48452bf5eb435dd0a161e99db687bc
MD5 a1158b6dd5766b37146c96fec45e0320
BLAKE2b-256 69f895f21dcc3384ab968d7d2f17ee8b3559d5d4b1e8d2286b4faa048f5212dd

See more details on using hashes here.

File details

Details for the file threaded-4.0.8-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: threaded-4.0.8-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 741.6 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 176e6ccb1f319105ff9bf562d51f42bef3446e112836541b7ad71fbdb8d169da
MD5 a5b711cd8595df74ce9eeef3af27342f
BLAKE2b-256 acbdfa65598d0e9089d9eb3539d6190a346d6c24543b9f0daa7ed472fddb73de

See more details on using hashes here.

File details

Details for the file threaded-4.0.8-cp38-cp38-manylinux1_i686.whl.

File metadata

  • Download URL: threaded-4.0.8-cp38-cp38-manylinux1_i686.whl
  • Upload date:
  • Size: 681.4 kB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8-cp38-cp38-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4f101a538f4eb5c49bf13dd3d81fc9e11fba8a7f6da9b1f72761e6f2667d04b6
MD5 3b0491fd0b9e8567e073d3558c24d64d
BLAKE2b-256 b21eaf1d313e2e8b179a70c578f2804aa188cb13efc6e59961c38bdddbc794bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.4

File hashes

Hashes for threaded-4.0.8-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 b2017b82c3861a9773ccbfa529cbb37dbace0d5c1cf99332485635f10cb7af88
MD5 4ced10d99311bbbea6f1b9f303ea5370
BLAKE2b-256 41c5f45d65de8f5ed00d198b0297a2cb0134d01c17859bd4e6e42dfa2aa45e2b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.4

File hashes

Hashes for threaded-4.0.8-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 9a14d12ebf582bed90d2ce90cd75c1ff57f837ad0ab75d5efa94a78d0dbcb4f4
MD5 be3a52a4d3c905a99af702b00e0108d1
BLAKE2b-256 1a82c49b839df7f9faee2683843fb31a63fb833b2a46717f30e2aab675f0d5eb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ac6655f21a2e5248f8616b49a75c93acca1c4f11778181df0a04ac490eb4359f
MD5 28928962e314ee5fc781f6dbf55121bb
BLAKE2b-256 1b2b9452e13c900705e5d6f73ec4fb00f266f7b7208b6e1bfd8968eec93cea81

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 5db8907215194eeb8daa97eadbd38f88ea1f2ec3bca25ca8e6db1725ad115c71
MD5 3350421018eb782eb5de7759fc5c6d8f
BLAKE2b-256 f8a683bde74fb135c1c0bf46aac374c88608173853802f9a675502f731df1b13

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.8

File hashes

Hashes for threaded-4.0.8-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 af1ffb73c0fa8020e8b9508f08744336409f197bf748fee399a9c984e1626185
MD5 727325ca0637e747aad2cdcfecea9c71
BLAKE2b-256 59b359b3ace223ce91ad90ad6681c19fc60eabdb66a01bed463ac5e9d314acf7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.6.8

File hashes

Hashes for threaded-4.0.8-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 90a42b91afac2a8c1f15dfd5e1b7d389f7c938705c194a8e18118a5b69ee4701
MD5 55c154068968e3745646d3b1132534ed
BLAKE2b-256 2e494e602991bd99859cbbd5ce6ed7ba5e1a3c1d15e0a28fe6c9c382dac22455

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 679.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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 317492f527098be4d9e9bde0ced05c6cf41114c5dc9b0d42ace32468bb99710a
MD5 21cb580a81d70ffff2c680e3c34fc650
BLAKE2b-256 c9ef7990a3484a7190b541b888ddbf8181db30dafc48bd8eaa07d6ef0fd5e985

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.8-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.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.1

File hashes

Hashes for threaded-4.0.8-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b91c1e86eadc9753bd1ec33640543cc3caac3d9134cc72cfe63e1e325ffcc8e4
MD5 29153ac11bed6166d5a0c57c735ce08f
BLAKE2b-256 b94760c94474e267940ab92a7cc82d481cab47216c6375f7fed04fd3fca60fc5

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