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

Uploaded Source

Built Distributions

threaded-3.0.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

threaded-3.0.0-cp37-cp37m-win_amd64.whl (186.0 kB view details)

Uploaded CPython 3.7m Windows x86-64

threaded-3.0.0-cp37-cp37m-win32.whl (158.4 kB view details)

Uploaded CPython 3.7m Windows x86

threaded-3.0.0-cp37-cp37m-manylinux1_x86_64.whl (691.3 kB view details)

Uploaded CPython 3.7m

threaded-3.0.0-cp37-cp37m-manylinux1_i686.whl (637.4 kB view details)

Uploaded CPython 3.7m

threaded-3.0.0-cp36-cp36m-win_amd64.whl (186.1 kB view details)

Uploaded CPython 3.6m Windows x86-64

threaded-3.0.0-cp36-cp36m-win32.whl (158.3 kB view details)

Uploaded CPython 3.6m Windows x86

threaded-3.0.0-cp36-cp36m-manylinux1_x86_64.whl (690.8 kB view details)

Uploaded CPython 3.6m

threaded-3.0.0-cp36-cp36m-manylinux1_i686.whl (635.3 kB view details)

Uploaded CPython 3.6m

threaded-3.0.0-cp35-cp35m-win_amd64.whl (172.0 kB view details)

Uploaded CPython 3.5m Windows x86-64

threaded-3.0.0-cp35-cp35m-win32.whl (146.5 kB view details)

Uploaded CPython 3.5m Windows x86

threaded-3.0.0-cp35-cp35m-manylinux1_x86_64.whl (663.3 kB view details)

Uploaded CPython 3.5m

threaded-3.0.0-cp35-cp35m-manylinux1_i686.whl (609.8 kB view details)

Uploaded CPython 3.5m

threaded-3.0.0-cp34-cp34m-manylinux1_x86_64.whl (625.7 kB view details)

Uploaded CPython 3.4m

threaded-3.0.0-cp34-cp34m-manylinux1_i686.whl (609.5 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-3.0.0.tar.gz
  • Upload date:
  • Size: 47.0 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-3.0.0.tar.gz
Algorithm Hash digest
SHA256 239f5ac3a3d08e372641d7a011e65c67c904a6fa302114ab69e967d3d6b4bb8b
MD5 cec7a16218b759f3b0ce87b506d81b4e
BLAKE2b-256 8b19ec0d795879a8c2e8efbf6a6a2583109d577b9b93e40ca908751e1a93a3ce

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.7 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 CPython/3.4.4

File hashes

Hashes for threaded-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1118642f5748fa0854f62854c625be3bcd84bf03fc4dc3d2754d2ac6ad7779a
MD5 6f1af0f18739e04861906064b31b0170
BLAKE2b-256 8f77438888ab115daee2eeb1939c7081e4eaf99ad6762e30fc306bae6dbe57a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 186.0 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.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for threaded-3.0.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 84e84c66ecc7721afa043c523dc5cd4298d828f0ccdbb914fdafd6bf940665a5
MD5 04d9646aa2d4f44f6b17362ec6c0227c
BLAKE2b-256 9470002f395bfa8ae159e14d431b100628f65c6ccbdde7c68742aea84fe1979e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 158.4 kB
  • Tags: CPython 3.7m, Windows x86
  • 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 CPython/3.7.0

File hashes

Hashes for threaded-3.0.0-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 f0177d4f94d82ded9910c9db5c9349c46f4cd8c86888b28b800f850bc976e7bf
MD5 6b1885c387c57f8672e88b19e295eb17
BLAKE2b-256 351f36475d05679bffd1498d6122c55d694226b42ff3c68681d9022275e8a7c2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 691.3 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-3.0.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a833b5165221de5468316f866d77df7d7e9696272d04da32129f24811cd27425
MD5 9f248df52974b97ece228d26addb8ae9
BLAKE2b-256 9a8ba1294b04c3f1c99b2f44452fb19c61491a92167143b8f4caa0f73db27d45

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 637.4 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-3.0.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 2ea0418cd740a2ff6856ccc45ca2354150219a876c6a8bde2a424b28eb81145c
MD5 6d4027b73ed484347c5ae70a0a3483b4
BLAKE2b-256 f9bd9206dbdc96f898458c811c2cef81b6f8b0ba3a61c8a1af55b9573b51bdf7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 186.1 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.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

File hashes

Hashes for threaded-3.0.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 4981e82c484742a0bb3cae6d6bd19bee7021ab0a8df0e8cf42b195dd3201b11f
MD5 b7f011a306ecbb225a4cb9f6cf46ea54
BLAKE2b-256 684b136f0a51944619f98f4dbe97f4f76504be04ceb76f5f0c1dff81cea15c4a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 158.3 kB
  • Tags: CPython 3.6m, Windows x86
  • 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 CPython/3.6.4

File hashes

Hashes for threaded-3.0.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 d165f1725ecc7f6ff2787efb55b34132fffb731db4d95ccffa199bda4c411143
MD5 028e540c355b170fb6946e768aae18fb
BLAKE2b-256 56aa3a01337cb77fcf36321ef93224615002f7aa71ea068dc362fca677df8f1b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 690.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-3.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 26c065d7a3eae960831a6a7b15ed9747c8d369cc969e7f84629521c402fd1dfc
MD5 58a1196a46171b523c86c0208bb8e2a2
BLAKE2b-256 7af60002d487e0f3f12b45dda26fcc2071628cad40267ca673c59b527619d087

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 635.3 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-3.0.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b936b366420fb958830c5015c6861e8db3fb8b7a88662fa67d44ec142c85e35a
MD5 315f1f68042b0a2e1318d60604353c12
BLAKE2b-256 86ff4cc40063d05d7a344265f393a23e1c040b7f45a956e12ff6d53be68d141d

See more details on using hashes here.

File details

Details for the file threaded-3.0.0-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: threaded-3.0.0-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 172.0 kB
  • Tags: CPython 3.5m, Windows x86-64
  • 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 CPython/3.5.4

File hashes

Hashes for threaded-3.0.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 553c8a46b702d094e846177dee84ad24d2176e341ce3d0334b16f2f78abc0de8
MD5 1e1eb9202ad39d3e8617bc5a06c3ba61
BLAKE2b-256 bff7ffde5cd28c234d151168c179c9997dc211fe6620070d33f8cba7ebe2863e

See more details on using hashes here.

File details

Details for the file threaded-3.0.0-cp35-cp35m-win32.whl.

File metadata

  • Download URL: threaded-3.0.0-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 146.5 kB
  • Tags: CPython 3.5m, Windows x86
  • 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 CPython/3.5.4

File hashes

Hashes for threaded-3.0.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 9723ef028528a6af1895d395006253e43aa0c38e279a068f01bb6fe7a720783b
MD5 5ca94dbbad00cd89727a2496ac2843c2
BLAKE2b-256 094c32f7c31c7e17fc9224e9d0feff8a077f75b07b9dfecafad333fce0095e8d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 663.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-3.0.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7b38cc7fa2271bd79a79b4eb883949fc5a3a50de7bb87fbada58064da0bf7cd9
MD5 936bf8d1320bc5b131a2d36372f5ee03
BLAKE2b-256 23d6cf0c5f81a9fee6054ea7720a01dcf4616297e1a5a112d6f3e4bc1ee1af8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 609.8 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-3.0.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 63639949eb1900f9f213bcdb73e6e1568f4b4f28e657176b51374bc145b8e9ff
MD5 dbb4f982198507615473a3925854036c
BLAKE2b-256 3b6d1a720a191ce65ad9658e0eee02ac0ef4bb13c886af85da826f7a97bd3fd3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 625.7 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-3.0.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6f6f37936ba9f7c7b4c80b28e20008f1aaedae0f87125a50d1a7ee9b49d94f53
MD5 dcca1c67c35f326f165050f5d8f11b9e
BLAKE2b-256 bb5864dc9ce1bec319336e2a51ab4ba3fae8f0fe0e6e4095e3efda4a513651e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.0-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 609.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-3.0.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 55ce92c193f4faeb6b071de359c4fbca819fceeb17679c1a440cf2ca34036a69
MD5 c01b9aa46740e12f1ef07c5f1da8d6a9
BLAKE2b-256 b115e2c978cef79adc88c0baa0438c108f3ffb10b734b298e3bf3433a02eb911

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