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

  • 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.1.1.tar.gz (48.4 kB view details)

Uploaded Source

Built Distributions

threaded-2.1.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

threaded-2.1.1-cp37-cp37m-manylinux1_x86_64.whl (821.1 kB view details)

Uploaded CPython 3.7m

threaded-2.1.1-cp37-cp37m-manylinux1_i686.whl (756.9 kB view details)

Uploaded CPython 3.7m

threaded-2.1.1-cp36-cp36m-manylinux1_x86_64.whl (821.1 kB view details)

Uploaded CPython 3.6m

threaded-2.1.1-cp36-cp36m-manylinux1_i686.whl (754.8 kB view details)

Uploaded CPython 3.6m

threaded-2.1.1-cp35-cp35m-manylinux1_x86_64.whl (787.3 kB view details)

Uploaded CPython 3.5m

threaded-2.1.1-cp35-cp35m-manylinux1_i686.whl (722.3 kB view details)

Uploaded CPython 3.5m

threaded-2.1.1-cp34-cp34m-manylinux1_x86_64.whl (739.5 kB view details)

Uploaded CPython 3.4m

threaded-2.1.1-cp34-cp34m-manylinux1_i686.whl (722.6 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-2.1.1.tar.gz
  • Upload date:
  • Size: 48.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1.tar.gz
Algorithm Hash digest
SHA256 e3b9bf32fa7e221945ec1f02bd976aeb57b147f5d8b41b2a73d62a2bc7102658
MD5 40066102362d3a90da7b5683be700371
BLAKE2b-256 79bdc1a2d904341efedec37d966a8b8aa1fcffbb0a4094702a4aca8f2b513e19

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ac62bb167e4844995728244a7320328eec0badedbaac29c03c308bf828788d4
MD5 8e0715b5857f51f78b9a133d97e8691d
BLAKE2b-256 7fca70f5a9b7575a2ccd4e5ff63811153a4cd23445d30ae6af909c522d969682

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 821.1 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 617d8d2d15b4eb59a122b1b3dfb5f4db3949ce9d9246d901720b4457281bdab1
MD5 07f187d4a0d7e525c04f6db46e7b95a1
BLAKE2b-256 968e1f411df55616cf3cc7bedfeb85f5eec5bafe921db8780870b5017103dc45

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 756.9 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b5b62dae796b8a9d2ecad9d1033ff644996b60399d738ea7f3b117f0a6091883
MD5 b28474a13d3bade8a77401cbd787c48f
BLAKE2b-256 4c9a39e11a22cf26579a791032c49adf90af5bbcf3f3fcf2d5a757d33f7ba0fe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 821.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f8a0d6bdc30c983d38fc46d2f3f0a8427d21182e63fd982b8fb7f16c8edbc965
MD5 548a77235ebf3aacb02de85ef729bcfc
BLAKE2b-256 35b0ab5f01e39c8ff6522891d3e9e63d8bccac3bec29d11122398b3b60a73957

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 754.8 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 07e0fc92efd4191dc537b74a6deed74797bb402195217389eebfe60dfe41cd41
MD5 7bcb61606fd38f81956f400c461dc0aa
BLAKE2b-256 4a0b460fb27ef6e1a079485a07883a4eb7935beeed85aec474cc95e77d6dfff9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 787.3 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1b8f8fb2f1791df7d491a896eff8bc7fa808122b4fd49a60952be6b665a8b759
MD5 3e4a127b0195d9e066f86538cbbbebb8
BLAKE2b-256 9f236fb5eb7ac72cb6448faba2a52ea59a5291344f172c76f13cf7e49e6d8dc7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 722.3 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9d5ad36769457505105c691a0fd8346d37dba667fe84ab98d0a3a104422709c5
MD5 b6efb5bae4f93718b8e9d0765662fc8a
BLAKE2b-256 29b2e7c9ffe104bfefca5cfa2ff13b03b83d9ea4d188409bdaa254d4f7bfcb17

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 739.5 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e101d3c55fc8b8f48aea8862fc4302520a6d9c4e410334e02ce4821aafd4260b
MD5 6d6acfb3acbbe68f821cde4e9cb0d109
BLAKE2b-256 d8a13a8cf65b0573446d566baf2feb3584ece9667f9556b04d185c080bc92986

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.1.1-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 722.6 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 PyPy/5.10.1

File hashes

Hashes for threaded-2.1.1-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6b4db5824295ee155d14cf9e19c49b824e02f0078450b93148ac2dbf7bfb415e
MD5 9df69b3f9ab80cb451f7a736428d03e7
BLAKE2b-256 75fc3a387dc2c4614b75db174fd5a2e44e75826a0bccbac352f8ce948b6ed252

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