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 2.7
Python 3.4
Python 3.5
Python 3.6
Python 3.7
PyPy
PyPy3 3.5+
Jyton 2.7

Decorators:

  • ThreadPooled - native concurrent.futures.ThreadPool usage on Python 3 and it’s backport on Python 2.7.

  • 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 Python 3 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)

Python 2.7 usage:

@threaded.ThreadPooled
def func():
    pass

concurrent.futures.wait([func()])

Python 3.3+ usage:

@threaded.ThreadPooled
def func():
    pass

concurrent.futures.wait([func()])

Python 3.3+ 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.3+ 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-1.0.2.tar.gz (288.2 kB view details)

Uploaded Source

Built Distributions

threaded-1.0.2-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

threaded-1.0.2-py2.7.egg (19.8 kB view details)

Uploaded Source

threaded-1.0.2-py2-none-any.whl (23.2 kB view details)

Uploaded Python 2

threaded-1.0.2-cp36-cp36m-manylinux1_x86_64.whl (713.4 kB view details)

Uploaded CPython 3.6m

threaded-1.0.2-cp36-cp36m-manylinux1_i686.whl (671.2 kB view details)

Uploaded CPython 3.6m

threaded-1.0.2-cp35-cp35m-manylinux1_x86_64.whl (694.6 kB view details)

Uploaded CPython 3.5m

threaded-1.0.2-cp35-cp35m-manylinux1_i686.whl (654.6 kB view details)

Uploaded CPython 3.5m

threaded-1.0.2-cp34-cp34m-manylinux1_x86_64.whl (710.7 kB view details)

Uploaded CPython 3.4m

threaded-1.0.2-cp34-cp34m-manylinux1_i686.whl (667.5 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-1.0.2.tar.gz
  • Upload date:
  • Size: 288.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for threaded-1.0.2.tar.gz
Algorithm Hash digest
SHA256 ccebc6ae37918705899a59fd023a55c0f3ff54996b87ba4f92c248fb8d847665
MD5 2c09cf40739f74799daf893c8139bb70
BLAKE2b-256 1ed2980299e2087605cf815d9f53cbd46d15f64271dcceb497fe1b43401224bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 88573fbbbc72b31a20133f0a476f2655e1e1dea1b334484f3a0ef03368cf3013
MD5 1f32b09d4248397f5354ed0cedf3dfd5
BLAKE2b-256 8fe071da3470837892f906ff94bfa4e9654f2281e5089ac5e9b39d152776b257

See more details on using hashes here.

File details

Details for the file threaded-1.0.2-py2.7.egg.

File metadata

  • Download URL: threaded-1.0.2-py2.7.egg
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for threaded-1.0.2-py2.7.egg
Algorithm Hash digest
SHA256 c62c103be56bb9b1995317ad4886efa9a84c8546a8cda4b2e798deee17812f25
MD5 e7f9a835362d7536aefe8c045b548824
BLAKE2b-256 506e57b53e80c4868101d4da3332dbab944b5f115804eadcbd5f589253ac1a6e

See more details on using hashes here.

File details

Details for the file threaded-1.0.2-py2-none-any.whl.

File metadata

File hashes

Hashes for threaded-1.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 0d1b47ba1dd857d1f34cdf31f435de0316937613bc9ecd06670defbe2288ebd6
MD5 2b5cf626a275f203da6d36bc8c35f33f
BLAKE2b-256 e66f9cd9550fd28719f7d46cb04d5e9aa6bc28b562663b5fcf21e057bf9e0208

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4d19e8c9524b691e5d34de0a3dfe4619f242740d34e73acd53066b7c88e70d40
MD5 9aa037b9590434638b91b9b79594abe6
BLAKE2b-256 5b1904b98de00e9a4fcdb8aaf4ec52e7e6483a0e17f844af8683a86c06986d58

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.2-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8f4a40e6f6cf159feeba1ef66b2b7266469b5847da7319098f8f89c20bbb51b3
MD5 789b6756bad6fee106fa2539febde433
BLAKE2b-256 e78a989c9067e4cbd8ce08a7f1c1829e3c8ecfd3556abca88447d22b34a284c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c5d1fed6ab4dc06f8c92492b0fbf3a38854d771f039946f4495fabfad43bada2
MD5 f4817833241d97cd85cb81f82e86f6ba
BLAKE2b-256 d5a44fa708a499a44f7030b83e85eaa07c6ae15fc960189226ad9206fcff1881

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 60a87b0b9e56a03bc97946d9e243d8e0e54c8f6041e7e6530ae98b44897741bd
MD5 fa7c9e4bf79ebce0b1db8cea11012ab8
BLAKE2b-256 a1e8733d69b461c9c48de5d4e817ce12a81b6b10ee35d5f51a3adf85840e4604

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 835dd0337121e01a0b56e6a4f94094651fc8aff8ba556afed1d2171fb1df200e
MD5 fec25a97a7f696a4ac6b989cf2835a03
BLAKE2b-256 c4f3ba1d5510cd943b4f1edbc07d0689eec93a4c56ce4f6a52a2ed10a6ec0100

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.2-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9c9a6592892f2d265d06eebf111a138d61012da362313aebf80414c36adee076
MD5 417fff6c4349b1167e379aad7922e45f
BLAKE2b-256 5b41ef3e4da0435276787e512d6d45c44f0a678d757128a15d4ee9d2183386fb

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