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

Uploaded Source

Built Distributions

threaded-2.0.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

threaded-2.0.0-cp37-cp37m-manylinux1_x86_64.whl (610.3 kB view details)

Uploaded CPython 3.7m

threaded-2.0.0-cp37-cp37m-manylinux1_i686.whl (608.6 kB view details)

Uploaded CPython 3.7m

threaded-2.0.0-cp36-cp36m-manylinux1_x86_64.whl (610.2 kB view details)

Uploaded CPython 3.6m

threaded-2.0.0-cp36-cp36m-manylinux1_i686.whl (607.7 kB view details)

Uploaded CPython 3.6m

threaded-2.0.0-cp35-cp35m-manylinux1_x86_64.whl (594.6 kB view details)

Uploaded CPython 3.5m

threaded-2.0.0-cp35-cp35m-manylinux1_i686.whl (593.6 kB view details)

Uploaded CPython 3.5m

File details

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

File metadata

  • Download URL: threaded-2.0.0.tar.gz
  • Upload date:
  • Size: 17.0 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.0.tar.gz
Algorithm Hash digest
SHA256 7492ee5b16070f7a34729944b190a3cc2b5ffffb7ea476d10091cd35cf02e619
MD5 a7c70faa6f63ea418bb74c459fea6bdd
BLAKE2b-256 096119402b8de3dab5b14f04759490eb757a4fbb605d432487cfbcafa41dcd88

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a4c6d79aa25727dd0fd6d8b6c862420a3dcd0783d44fb4f64c7ac4d427988cd
MD5 f25669cb17944a44d9bd5e7f2390412c
BLAKE2b-256 86cd890b5a6db57a1e59ccccacabaff8c24fac98bf30aee8b3ff5ca9ffcafa98

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 610.3 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.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 85e4f2b0a1e276a83f44ef87b8207e08255139317b7ce94b897dfefd07889570
MD5 85bd42a4888072773672279ecfe2b644
BLAKE2b-256 bfaf877d0ef57e8099a3148d5b326be254f44f888b94906771e63af5cc0f5093

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.0-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 608.6 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.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 5cf29e5de69d01cc20ec75953d991bea96fb14c70592c2c1d7da72eb36d69aa2
MD5 c28b870566cc87f4f7a706469ba025e2
BLAKE2b-256 f8ed8cb67d999016a2f8b2c56e24f85b51bcbccb6082a220a53ccd91fce0f0c4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for threaded-2.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 521592a122234fce52bf51117d5605b593873cf0038ed996f0411c86711d34d7
MD5 c1e7277eeac8e6ef31a02832de136dc0
BLAKE2b-256 11d426a4d7a362e1fb1648a77d38c22bba4b732e802d753709d9b26b720094fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.0-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 607.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.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a759e528742761612ed72f26f6df7fa362de95ea32b376741032a50b19540d1e
MD5 9033d94ecede98cbd7e238b7bf6e5e66
BLAKE2b-256 cc82ae01a6aad5b2f206c3a1458c0ff64548926efa6e36c52012876cba6f38bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 594.6 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.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3586d4c96001a0909d210cd774d00b98de8265a7283f5ad5f51f89c1fdeaac1d
MD5 69d857304fba6396d640cafc8cb88b10
BLAKE2b-256 5d527aa2c0b07fbafc28dc6b8c2a0749427ba1603fd6924b8346154d7e175d50

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-2.0.0-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 593.6 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.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 45e256fff836d9da61d037f1f5f9d3052b2320fc2e8bd42118666a2aa94aceed
MD5 af0684f143f735ab18b5f5cb1443ba76
BLAKE2b-256 91b61d2e64fb2fae8968e927db6ee7732628341d940bb22de46fdf5747ed91f8

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