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 https://github.com/python-useful-helpers/threaded/workflows/Python%20package/badge.svg 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. GitHub actions: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests.

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

Uploaded Source

Built Distributions

threaded-4.0.9-cp39-cp39m-win_amd64.whl (186.2 kB view details)

Uploaded CPython 3.9m Windows x86-64

threaded-4.0.9-cp39-cp39m-win32.whl (163.0 kB view details)

Uploaded CPython 3.9m Windows x86

threaded-4.0.9-cp38-cp38m-win_amd64.whl (187.4 kB view details)

Uploaded CPython 3.8m Windows x86-64

threaded-4.0.9-cp38-cp38m-win32.whl (164.7 kB view details)

Uploaded CPython 3.8m Windows x86

threaded-4.0.9-cp37-cp37m-win_amd64.whl (182.5 kB view details)

Uploaded CPython 3.7m Windows x86-64

threaded-4.0.9-cp37-cp37m-win32.whl (158.9 kB view details)

Uploaded CPython 3.7m Windows x86

File details

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

File metadata

  • Download URL: threaded-4.0.9.tar.gz
  • Upload date:
  • Size: 29.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0

File hashes

Hashes for threaded-4.0.9.tar.gz
Algorithm Hash digest
SHA256 909ad4ca67a77ed388e4dc6addef689834de286f441ef38ef7de19e1031c6bf0
MD5 6d20b9ce81e4cf4e50eae67e58a78ab3
BLAKE2b-256 381033c579d6e9d21bdf3c4ff8c95715d5e38dfe9c514837079e9563747c9233

See more details on using hashes here.

File details

Details for the file threaded-4.0.9-cp39-cp39m-win_amd64.whl.

File metadata

  • Download URL: threaded-4.0.9-cp39-cp39m-win_amd64.whl
  • Upload date:
  • Size: 186.2 kB
  • Tags: CPython 3.9m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0

File hashes

Hashes for threaded-4.0.9-cp39-cp39m-win_amd64.whl
Algorithm Hash digest
SHA256 766d95bfedb5e7050b3492d1aabaaa7dd93a521fcc3ed88837e6494be61f624d
MD5 3ac1c25229f4538d739e5e5ebd2823d8
BLAKE2b-256 ca268f32d58c2a05030c197d60291ec9d6fd82f3a1f10cd159fc9353f98a4f21

See more details on using hashes here.

File details

Details for the file threaded-4.0.9-cp39-cp39m-win32.whl.

File metadata

  • Download URL: threaded-4.0.9-cp39-cp39m-win32.whl
  • Upload date:
  • Size: 163.0 kB
  • Tags: CPython 3.9m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.9.0

File hashes

Hashes for threaded-4.0.9-cp39-cp39m-win32.whl
Algorithm Hash digest
SHA256 11edb35d1ff1c3e42d13563e77f0b2d23f239ba7a6cb0112aaf227dd50794ac8
MD5 973ad43a1ac08db24ca535f848e97bc7
BLAKE2b-256 4f4afc8afbd18d1e96346f68bfb523772a30b13df781efbf2f4f02951727a658

See more details on using hashes here.

File details

Details for the file threaded-4.0.9-cp38-cp38m-win_amd64.whl.

File metadata

  • Download URL: threaded-4.0.9-cp38-cp38m-win_amd64.whl
  • Upload date:
  • Size: 187.4 kB
  • Tags: CPython 3.8m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for threaded-4.0.9-cp38-cp38m-win_amd64.whl
Algorithm Hash digest
SHA256 73b59edec3e11f91b9da5f03665251067e035b8bd8fe4106161bdda70bfc71ab
MD5 f66b30585bcbbb2ebacd73a8f7a21955
BLAKE2b-256 689d25af508010754a387a0fab9c3b8b69badb7e6c8b3fc862deabf0c0717ffd

See more details on using hashes here.

File details

Details for the file threaded-4.0.9-cp38-cp38m-win32.whl.

File metadata

  • Download URL: threaded-4.0.9-cp38-cp38m-win32.whl
  • Upload date:
  • Size: 164.7 kB
  • Tags: CPython 3.8m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for threaded-4.0.9-cp38-cp38m-win32.whl
Algorithm Hash digest
SHA256 daf57cfd8f0e9b0148d9ff6609f959bf3b7a42459bd85840fc6d56b441a407c6
MD5 88bdc5768828e320db359429a1ff54a7
BLAKE2b-256 cdf63fba704242b61d4cd75a3c03bb46cc83cebf1e1f836e230ecd949e25bc1d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 182.5 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.7.9

File hashes

Hashes for threaded-4.0.9-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 1bf0c9149aca0024bed1cd39f7ef85d9809e912a365e07ee035952e80a97ddfb
MD5 97908e5fda87065df4f7f27e2bad334c
BLAKE2b-256 c6bf1a420590d9100238843ecfcb8779404acc635a5176cc9e808e8de447d277

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 158.9 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.7.9

File hashes

Hashes for threaded-4.0.9-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 8fc4c6049fe0562e9b35b081d5b468949fb9047ed8dcd57272cf5c57b991a736
MD5 91ef92bf7f16cf42bd7cdad04a6734d7
BLAKE2b-256 ec6e42bc29c356e818061639fa6fad0aac5d71c002edd5fa44f8c8fdb8730246

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