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

Uploaded Source

Built Distributions

threaded-4.0.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

threaded-4.0.0-cp37-cp37m-win_amd64.whl (187.4 kB view details)

Uploaded CPython 3.7m Windows x86-64

threaded-4.0.0-cp37-cp37m-win32.whl (160.1 kB view details)

Uploaded CPython 3.7m Windows x86

threaded-4.0.0-cp37-cp37m-manylinux1_x86_64.whl (686.7 kB view details)

Uploaded CPython 3.7m

threaded-4.0.0-cp37-cp37m-manylinux1_i686.whl (636.2 kB view details)

Uploaded CPython 3.7m

threaded-4.0.0-cp36-cp36m-win_amd64.whl (187.6 kB view details)

Uploaded CPython 3.6m Windows x86-64

threaded-4.0.0-cp36-cp36m-win32.whl (160.1 kB view details)

Uploaded CPython 3.6m Windows x86

threaded-4.0.0-cp36-cp36m-manylinux1_x86_64.whl (686.1 kB view details)

Uploaded CPython 3.6m

threaded-4.0.0-cp36-cp36m-manylinux1_i686.whl (634.1 kB view details)

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: threaded-4.0.0.tar.gz
  • Upload date:
  • Size: 40.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.0.tar.gz
Algorithm Hash digest
SHA256 89870efe373862956ee6acd77689604bb2aa69ed95e414599e9c067918045bd3
MD5 bfd5d78ff633d8ab5836f682e3d3c2dc
BLAKE2b-256 7ae71fd9cdf4aed55120dc7ffdd0cd8c6ed5df7c8bf52eed1b62df06a52442e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 617c6a93e5fe9ec2963e627fb74142f6539cbb9f79c8c73e870f0112b5fe0366
MD5 ec52a8cfa658cd39a5feeead87f4e8bd
BLAKE2b-256 10e2e83847843c338317b05fc128b89a7b29d3f77f9ca654e5f865f3d46ae598

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 187.4 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for threaded-4.0.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 5e03570fa7f24842a80a59185c864935f0068aa7ca82160459e6b00d719cb3ed
MD5 75e27f764fff2a3a50c5aaf67a5c680f
BLAKE2b-256 706122a20799cf25e9feeda23b1ef09908a7f2b5fdf0b0393277437545ab1e9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 160.1 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for threaded-4.0.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 026615f2c4f315841ea4066b5a75ef52aeb47c7c761f55f5955069d87100a629
MD5 bedb336b9d3a770d3b13b86bd01698cb
BLAKE2b-256 689c1d4c074b7a0634c5449c1fe636a64477ff50f58de35044bf2fb4960b326d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 686.7 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3457304bfec596b4f0d24bb37d999f107d9d0bf0956f46c2fc770b7d60d12035
MD5 ec66357b620aafe00cc2eb4f006056c1
BLAKE2b-256 f856b252bc0d0b39b2d2449e8eb0a172193f7df436180a7fe6c77c0bfa622cd8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 636.2 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 52f9b229f074bbf278c80647c10180e0d5ffde9470d86e5f078e8e86d34648f6
MD5 b91a565c4b8788c59c8f5f52d867cd62
BLAKE2b-256 57fc75112ed540bf5ef9a0f17d75ab2d8ab0b395de17f43ea723fad5ca2a978c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 187.6 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

File hashes

Hashes for threaded-4.0.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 5e6b39d4e0cee275f9dcfb928c221d50b026e7660e6ec7718555a5f11cba9e3f
MD5 893eab0872b4b5132e8833bd4129eb43
BLAKE2b-256 861c88cc7761b6810fa062a0cedb55d7b674475463616d2abfe99a28400e4af3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 160.1 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

File hashes

Hashes for threaded-4.0.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 19e4101c04783d6aff4d43f1b0f893fe4e72a4e428a2c9a3705c5b921fe3c7b5
MD5 ef6e7dbb3b6be9e99c1e49d91442a264
BLAKE2b-256 566e3366b6c2dbf9f81ad4c357678e1eac699fd1c91feb1b33f4003fa6b2f41d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 686.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a1cd34461945890a97514b8e55335102014e0e0499e6cc43d61cfff822f7e79e
MD5 34fce7debdb0f54d1565c01ee48eefd7
BLAKE2b-256 612213f4a906c643182568dc06db94c046a9912c1cae9273df72048f21c1e263

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.0-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 634.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 659649e7cec6b37872b2b6c95fb852262fc497d384e5598a2fbe6bfa17e78e85
MD5 a381ed267d0da9a1ab2ed8736944f1de
BLAKE2b-256 362c38d4a206088d6d9a9557a384e026a3e0b821791528d82decb229f85d0f15

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