Skip to main content

Decorators for running functions in Thread/ThreadPool/IOLoop

Project description

threaded

https://travis-ci.org/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.

  • gevent.threadpool.ThreadPool if gevent is installed.

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

Uploaded Source

Built Distributions

threaded-3.0.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

threaded-3.0.2-cp35-cp35m-win_amd64.whl (171.5 kB view details)

Uploaded CPython 3.5m Windows x86-64

threaded-3.0.2-cp35-cp35m-win32.whl (145.7 kB view details)

Uploaded CPython 3.5m Windows x86

threaded-3.0.2-cp35-cp35m-manylinux1_x86_64.whl (656.6 kB view details)

Uploaded CPython 3.5m

threaded-3.0.2-cp35-cp35m-manylinux1_i686.whl (603.6 kB view details)

Uploaded CPython 3.5m

threaded-3.0.2-cp34-cp34m-manylinux1_x86_64.whl (621.0 kB view details)

Uploaded CPython 3.4m

threaded-3.0.2-cp34-cp34m-manylinux1_i686.whl (604.1 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-3.0.2.tar.gz
  • Upload date:
  • Size: 47.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.2.tar.gz
Algorithm Hash digest
SHA256 4194b9c73ae08bb1d7256954d55f3de7bba2f2d6f8684ac68ef586cc408237bd
MD5 a51e90948ff1abc58d65a9f55c36f386
BLAKE2b-256 ee92f928e724418ed0f080b8e9881b1fcaf0fe1ee4b62109633e46aeb7fc9b78

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.8 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.4.4

File hashes

Hashes for threaded-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8f7a8cafaf8b11d24b071a9599a770f877e828f815d32055a1ad0c04a56c3a97
MD5 26c6dbc0e334dd7d551147765918a082
BLAKE2b-256 b0ee283fe430b45af757f826c92b0681ac582669178703deded9f14b1182e364

See more details on using hashes here.

File details

Details for the file threaded-3.0.2-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: threaded-3.0.2-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 171.5 kB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for threaded-3.0.2-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 3b9c08646c454b3451f2d0b8e8cd9ec430e6a4382f04cc07ac1a6d094e3b6c64
MD5 2f16d78e1e90f51b34dd9c566f8a7f63
BLAKE2b-256 d0cd5035267d72903dbc9fa18f088012bb5a08fd21c7e3bc890665c76f768b5e

See more details on using hashes here.

File details

Details for the file threaded-3.0.2-cp35-cp35m-win32.whl.

File metadata

  • Download URL: threaded-3.0.2-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 145.7 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for threaded-3.0.2-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 02df59e5da7cdfdf114fe59a842718111821e2187c748ed5d7edca437e83ef03
MD5 4b8d1b5f6b41c69d1f74b6091e48e481
BLAKE2b-256 3c9d98b30fb91b2266d4884ecfce548f98dec603354ce4ad6e4183d844e06fbd

See more details on using hashes here.

File details

Details for the file threaded-3.0.2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: threaded-3.0.2-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 656.6 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1ae977843fb8e3258976a2dedba34f1e84610b95cfa86fcd4c8e35a05b5279c7
MD5 646047e6a8b1e826da2b4185cd659e7a
BLAKE2b-256 be323e0e6fe5f6e55bbf3ab81161c3b5f33a6e9850c033453e22b749e3408be0

See more details on using hashes here.

File details

Details for the file threaded-3.0.2-cp35-cp35m-manylinux1_i686.whl.

File metadata

  • Download URL: threaded-3.0.2-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 603.6 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9b5b6947b26d8b713cc10630a89be4b185200cbbd1acc64ccc637a24a6dae88e
MD5 7af07c96fdf91f27e5264ea737c5d2d9
BLAKE2b-256 c01f46dfe21d5d17151452cadc8e0e2a23cf575475ddb9cc3175ed352da88f4f

See more details on using hashes here.

File details

Details for the file threaded-3.0.2-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

  • Download URL: threaded-3.0.2-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 621.0 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 11d7f7699261fe1a7481d94adb6c68c570f2e93e0fc109b2f3cf91f9708bdabe
MD5 b1a704afd389c0f28caed495181b40b8
BLAKE2b-256 cda48ea4e857650ec2f9e6ca21eaca38ac7f8db1b9d8835462c45ed7269eb578

See more details on using hashes here.

File details

Details for the file threaded-3.0.2-cp34-cp34m-manylinux1_i686.whl.

File metadata

  • Download URL: threaded-3.0.2-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 604.1 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.2-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f2c3d7bbbfb5de4df028ce29a5be5a7f693592ac7473025b27f67df1ea5381cd
MD5 818237dfa249e9153ba493f7424d5145
BLAKE2b-256 af99d56adceb8f8a05a4dbdbcad5d0a7f79b9ca482202d0a2bc54c87d9bbb760

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