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

Uploaded Source

Built Distributions

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

Uploaded Python 3

threaded-4.0.3-cp37-cp37m-win_amd64.whl (187.5 kB view details)

Uploaded CPython 3.7m Windows x86-64

threaded-4.0.3-cp37-cp37m-win32.whl (160.2 kB view details)

Uploaded CPython 3.7m Windows x86

threaded-4.0.3-cp37-cp37m-manylinux1_x86_64.whl (687.3 kB view details)

Uploaded CPython 3.7m

threaded-4.0.3-cp37-cp37m-manylinux1_i686.whl (636.7 kB view details)

Uploaded CPython 3.7m

threaded-4.0.3-cp36-cp36m-win_amd64.whl (187.8 kB view details)

Uploaded CPython 3.6m Windows x86-64

threaded-4.0.3-cp36-cp36m-win32.whl (160.3 kB view details)

Uploaded CPython 3.6m Windows x86

threaded-4.0.3-cp36-cp36m-manylinux1_x86_64.whl (686.6 kB view details)

Uploaded CPython 3.6m

threaded-4.0.3-cp36-cp36m-manylinux1_i686.whl (634.7 kB view details)

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: threaded-4.0.3.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.3.tar.gz
Algorithm Hash digest
SHA256 05202a20b11f229d003abbbea3b1f14875e8aed902ff895dea530a175204e895
MD5 1d25cebd2e4b05466291e98e94ae67ce
BLAKE2b-256 84543b2d94f0ff64956cfedb641a323971f5fb1287959330c222075931a4e857

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 72100969194bce14c173b2405cebdbffb194a9a88f9f291e66c2d58e529b905b
MD5 449b66d9b2284fc18e74956dd5c508cc
BLAKE2b-256 2c53a260f4850dfa8c23feff8f2f1c8912b9e66b28c44069aa6505404c39ec3a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 187.5 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for threaded-4.0.3-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 337789d144ca71c5f022053b0d2f2fec858e32f636051093e28f13da0afbb1cb
MD5 dd32aec68e6006a66f16b5f0b262f0e9
BLAKE2b-256 9aa89374007be70030f88185e602219d711f32fcab0cd35481f18f0d0adc86c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 160.2 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for threaded-4.0.3-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 24ebadb8f804e38a1355371a6b11fddf220cfaa8e104835bbbc435a7e59c9993
MD5 5b0782f79d35543d3c03fe77c3e38bac
BLAKE2b-256 3262f2266448f267fc2bc0de15c077f835e2435bcdcad8db6f3df2d2aba14452

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 687.3 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.3-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f2ecf874b4e9db6efbe6b3bf4a5757336890499aeb318fb773a8235d48a23a8f
MD5 69602c5aecf2e0252a21f2651eebd049
BLAKE2b-256 c2aff5db5fe8bb26b48d595d98f43a97997a299b1d62d843e4570508d94acc3d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 636.7 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.3-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d372a9a788f6e8a0d238092455ffbeb5df23e49c18e8bb0f13cad160d22fb055
MD5 7623df93ae2a0ddfdc92eca7a56ee87f
BLAKE2b-256 b3d02af368365db0a48e1a35452d2b717935e63d8ab40f9602b97f3207c229ca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 187.8 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for threaded-4.0.3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 b4e5837ae62b242ebb8f0c49dff17347cc9522a714a5569d416105a28ede3c9e
MD5 7eddc6c2a7fda5ae83f5e66948f8660e
BLAKE2b-256 40e4a7ef7f06964d6c1daadc8efbe7f0f768300b44e1a1be52dc93692c602e22

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 160.3 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for threaded-4.0.3-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 c9822a4407961bed622339204771e0053121bf87d6fc76d77c87b06f503a55ce
MD5 8da3dbc1583bbb6b0fe1c0728a973d06
BLAKE2b-256 e497e688838d6fdf86d3e7f97eb2dcc19962ea5e6bf8589fb66968b4b3fa0954

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 686.6 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2be93b1106439bd175702a5d912f42c525fc14ef510b58576361786cda598b86
MD5 0a046711888b92c9133e8b0819e63e96
BLAKE2b-256 792f07bb2a689fe73e113d696607e134416540eb843c7bd9f2cb8ee494fa9cd3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.3-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 634.7 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.3-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ce72ae077299f1d77117c15ff58324fae06732a1520ecacc47dee9eb42432e0f
MD5 eb0dcc79a85434498a0959060a9fe1e5
BLAKE2b-256 91a5cd552f99b2c1084e201cf377d45436cd7359eedb80e369cc93a1a7cc7cd4

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