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

Uploaded Source

Built Distributions

threaded-2.0.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

threaded-2.0.1-cp37-cp37m-manylinux1_x86_64.whl (712.1 kB view details)

Uploaded CPython 3.7m

threaded-2.0.1-cp37-cp37m-manylinux1_i686.whl (711.1 kB view details)

Uploaded CPython 3.7m

threaded-2.0.1-cp36-cp36m-manylinux1_x86_64.whl (711.7 kB view details)

Uploaded CPython 3.6m

threaded-2.0.1-cp36-cp36m-manylinux1_i686.whl (709.3 kB view details)

Uploaded CPython 3.6m

threaded-2.0.1-cp35-cp35m-manylinux1_x86_64.whl (695.2 kB view details)

Uploaded CPython 3.5m

threaded-2.0.1-cp35-cp35m-manylinux1_i686.whl (693.5 kB view details)

Uploaded CPython 3.5m

File details

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

File metadata

  • Download URL: threaded-2.0.1.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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1.tar.gz
Algorithm Hash digest
SHA256 1bbdf7d8dd98ee25d965c18f8a37e1e4d9e2610c111cc585db538fa93242d8f7
MD5 e8ab2e7aeddc2b9ebba16f5c1a847a5c
BLAKE2b-256 73e40d24028c2b4420af324ada89c9bf3e264b2acbd689a2a8348413e65fcb89

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7234370027b2722b660ef6d3df9a405e47f1688f9bef2fea6076d583518311cc
MD5 157898f90272012df66e9ba56aa40231
BLAKE2b-256 56e5cde49bf0489866833fafdba48641d62b55c1cb1612b1debf20fae983dc22

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 712.1 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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 84a56c91d2347b94a9de8fe17ef3ac78c2d0bb658417ad02827f01642dd01f0b
MD5 c70da77cd6ceace88efa3a72b23a24b8
BLAKE2b-256 e1bdc5a31ee998a32a4a40969afd9ee8f73b441b5592c0b2ced8522e32282af4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.1-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 711.1 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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 83ec43c24a2e9da2464d96936ab1a5b0b874f0bf82ebe9109f3df4274ff523df
MD5 cd5aa0f5c0e0c4f011b46111d6174d75
BLAKE2b-256 d748084b78a9d61002c596f37ad959d868493e901b6c68841d7e98ce74c1c88e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 711.7 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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a2f332ae2485a2d59128e4dbac5b025e7c03fc7a5c05aaaf1428bfe032386c3c
MD5 88eba7942382a9ebfdaab64c19288852
BLAKE2b-256 dc78bb834646ba86c7a11bab7da00342cc6215a40f8f6456661f0a10c53f78bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.1-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 709.3 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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 debf712748b6a71f5bfec8f97944afb5d6a160a932e28de8f024a82e688c6373
MD5 2b8a764d92b0700998ac8e1b67a914c3
BLAKE2b-256 c3cbc2487df2a84d9920ea1583654f4add609e73de5ea17d520f81810b8bddf1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 695.2 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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1f22c2244ab5fcade8b785baac7e7cf038c6ddd34a76a9d0f5a99ac4d4b5e23c
MD5 bf3d100339df4bada691b90e16d2fa52
BLAKE2b-256 58acdda0eea7c65d0dca883c779f7908a8cc899f75922869db162809019cb57e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.1-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 693.5 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.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 PyPy/5.10.1

File hashes

Hashes for threaded-2.0.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6813ed9787cdda541a3bf093a46b4356a9df29908cd1f0c4ecfe8184bbd4a072
MD5 e01430cfe96a2928f0f2f7f3fdd001d8
BLAKE2b-256 37e1b12506af4d3026368fc6cd9d48e98d5b65acaea07c1b08a769cc07782d27

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