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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.9m Windows x86-64

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

Uploaded CPython 3.9m Windows x86

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

Uploaded CPython 3.8m Windows x86-64

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

Uploaded CPython 3.8m Windows x86

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

Uploaded CPython 3.7m Windows x86-64

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

Uploaded CPython 3.7m Windows x86

File details

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

File metadata

  • Download URL: threaded-4.0.9.post0.tar.gz
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • 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.post0.tar.gz
Algorithm Hash digest
SHA256 bdd16374585ea0f904922318242f3e6ff46636c7390d75cd0bfe3d3f1c05b097
MD5 e84e6086e89f0399baeabd4a8600567c
BLAKE2b-256 0273c704a62bd0008cc49108d79985ac9760b4ab06a4458baec82765a154d172

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9.post0-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.post0-cp39-cp39m-win_amd64.whl
Algorithm Hash digest
SHA256 cac9af372f60ccf417085d7035ae7b89a6b9ab23ed94460efc174592f14669b9
MD5 004b3f1513ececfe0bcab27c2fd4f069
BLAKE2b-256 f4c4631ce66545e8ceeefbeb48e0426e35de42c0d5637b102dd70ec56add2911

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9.post0-cp39-cp39m-win32.whl
  • Upload date:
  • Size: 163.1 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.post0-cp39-cp39m-win32.whl
Algorithm Hash digest
SHA256 26e4fad757c31ce9fe5d68e1ce6165596b2764a3817ecd4926a470f3289ad83b
MD5 1243a5d3068bd615244f572f5541c5cc
BLAKE2b-256 c17d49b4d34973943ad745c8d28449551b8133492c9ff173880a30d35393ef13

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9.post0-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.post0-cp38-cp38m-win_amd64.whl
Algorithm Hash digest
SHA256 8a08e8bc9c309038b8b1b4c9533b017a3305dea01bccd931e2ca92e79f2f3305
MD5 1516decee3e4d631b871eb10811d4c0e
BLAKE2b-256 1888d849624093ac26665dc9fa7d385abc585f2cfd79052469d682b7aab583fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9.post0-cp38-cp38m-win32.whl
  • Upload date:
  • Size: 164.8 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.post0-cp38-cp38m-win32.whl
Algorithm Hash digest
SHA256 39cf9ca6c902c5421d249539085c02a48661aa32f834c47ae8622be355ded1c1
MD5 f199dd4d7ddd8ccc303ee2dbb1776683
BLAKE2b-256 73450fb4e84e0d87add0c36d9d43060a21626ace038c5ab66e8ac33f0ad93d70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9.post0-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.post0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 84b0ef58471e770eb7db5bd4aa0a10ed811b2a99181bdebd178a5dd14ee72bab
MD5 e61a1418b531b8fb2655dcefa76ce386
BLAKE2b-256 440ee99a9ea69b40abefede3b1461637e5e461d046f9dfea9f75becebf1e4069

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.9.post0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 159.0 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.post0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 0c74f9ffdd20a2838cca9230ba4a699812c259d28d99491ad2689507057528b7
MD5 968654d8711d4d295c3bfa20dc560ed8
BLAKE2b-256 328f76fe2dc2641cb0cb2830b9cc330c25d5116f9ec18c146cf85d39ac9e4810

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