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 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

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.

  • GThreadPooled - wrap function in gevent.threadpool.ThreadPool.

  • gthreadpooled is alias for GThreadPooled.

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))

GThreadPooled

Post function to gevent.threadpool.ThreadPool.

threaded.GThreadPooled.configure(max_workers=3)

Basic usage example:

@threaded.GThreadPooled
def func():
    pass

func().wait()

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.

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

Uploaded Source

Built Distributions

threaded-2.0.2-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

threaded-2.0.2-cp37-cp37m-manylinux1_x86_64.whl (717.0 kB view details)

Uploaded CPython 3.7m

threaded-2.0.2-cp37-cp37m-manylinux1_i686.whl (715.7 kB view details)

Uploaded CPython 3.7m

threaded-2.0.2-cp36-cp36m-manylinux1_x86_64.whl (716.9 kB view details)

Uploaded CPython 3.6m

threaded-2.0.2-cp36-cp36m-manylinux1_i686.whl (714.2 kB view details)

Uploaded CPython 3.6m

threaded-2.0.2-cp35-cp35m-manylinux1_x86_64.whl (700.1 kB view details)

Uploaded CPython 3.5m

threaded-2.0.2-cp35-cp35m-manylinux1_i686.whl (697.8 kB view details)

Uploaded CPython 3.5m

threaded-2.0.2-cp34-cp34m-manylinux1_x86_64.whl (714.9 kB view details)

Uploaded CPython 3.4m

threaded-2.0.2-cp34-cp34m-manylinux1_i686.whl (710.8 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-2.0.2.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2.tar.gz
Algorithm Hash digest
SHA256 b2c5e26e6b9c147660507769f778256af5443edf9f8a0b3fa7641eb31e4a343a
MD5 ae4f049d7a00cb4cdb345d8c1a264608
BLAKE2b-256 b4ce855af5649050a2fecbc282286531caba70a006122fbf2afd1c9a2b7d79b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6dd880301102c284d275586a3f28c0efd019ab907a8f1b23db237cc75cd6403c
MD5 6c187adc08df8e5b1e4be719cb4840ce
BLAKE2b-256 796a90191adafe27419a108205bbc078bcf5e308e3d862fe7e4898ff0fe7119e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 717.0 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6c13d7ff8b8afa8f4604b42b4e95c0ca9fa3e13e1440410530b4fe7055fd3af2
MD5 88f767fd2adfac4f5d99628fa86aa036
BLAKE2b-256 232d09482ab320efb4575c72596600379cd7fc8a0ae9b9b3da898504ddc1153e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 715.7 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0a06823effce2688e47ce364a0217463a20d3497fb5f004491ee7cd8f6c38a0a
MD5 9945966ddd224787953e43c480ccba77
BLAKE2b-256 95f71a2ca3049377f7218c51689a30e59bd1535f16ba496ec987f99e53b6f0a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 716.9 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 79b2c6cf83ba4e9a1b613c50d5b6cccfd35d149f87d9f3916cb65491ca33684e
MD5 ec5370b58d2359895477a9fa4a0b3b91
BLAKE2b-256 535053b50faad361760fa678138c7a547f42488135ef9eae5665af5835cba897

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 714.2 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ad8b416f06e5f896d78cf16bb27620f2cf68b3e47aab9513c4535eb7e7d5efb9
MD5 e374d13c936b0f2d8ea6725a477495f0
BLAKE2b-256 c6dabbb189f9b4b7b7dc385cc891c46847e529f4bc003141382604de905b92f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 700.1 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 be184c144d7d2ace8404054ebd0f6ba7396db53f3ecb44341a5f35a8c299cef2
MD5 4b0953a86e0bcf55cf60efedeeb84606
BLAKE2b-256 a96d5eef992f067120c09fcc2204f1f9aeab626425a23442357e1de72883aaab

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 697.8 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a87735270dd99ab3607fc480c7677b68f56707d9ae556f8a16ecbc007766e776
MD5 2730e8473353124d3119875a0f8c9f97
BLAKE2b-256 0373dccfe8c2f9d440da42d25816ab520e0bc6ab3a7105aef2f402ae3ae4b1f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 714.9 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 36a2aabd5a1d5e8c5c0abf82bdb12a91d53f5822a341c4c86c59cd2954178058
MD5 5f0512dad1910365c846ae0c3ca4097b
BLAKE2b-256 2bf3de1eb7a3d7ad67f9a82d3011c5355943c4bfc7c0af064ccb3857fa5ede6f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.2-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 710.8 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.2-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 194f6831132f05640bfb23276e5cd882e46f4579292cd0ff9601492760826ea8
MD5 1d6d873d308c409d394f33d01c078e7c
BLAKE2b-256 f74cd4c5d1dbb2e169c52b7464102bde0c465d46f3bb0710fb832f0a3cb067aa

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