Skip to main content

Decorators for running functions in Thread/ThreadPool/IOLoop

Project description

threaded

https://travis-ci.com/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.

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

Uploaded Source

Built Distributions

threaded-3.0.3-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

threaded-3.0.3-cp35-cp35m-win_amd64.whl (171.6 kB view details)

Uploaded CPython 3.5m Windows x86-64

threaded-3.0.3-cp35-cp35m-win32.whl (145.8 kB view details)

Uploaded CPython 3.5m Windows x86

threaded-3.0.3-cp35-cp35m-manylinux1_x86_64.whl (657.3 kB view details)

Uploaded CPython 3.5m

threaded-3.0.3-cp35-cp35m-manylinux1_i686.whl (604.1 kB view details)

Uploaded CPython 3.5m

threaded-3.0.3-cp34-cp34m-manylinux1_x86_64.whl (621.5 kB view details)

Uploaded CPython 3.4m

threaded-3.0.3-cp34-cp34m-manylinux1_i686.whl (604.6 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-3.0.3.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.3.tar.gz
Algorithm Hash digest
SHA256 ad8652ff96f607e5433e1a869a333d6edb8d2a61b530d4bc586581c9f1af44da
MD5 b6c9a86554b550f92b20754e8f4b1fd5
BLAKE2b-256 3ec74a1439f6f041d0d7e49246e7190c1ff6c73630a885319943715ced40dbd1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.4.4

File hashes

Hashes for threaded-3.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e55079b90aad4c1538778082866b5d071d4d59e3888464c02a88746d1295ab6b
MD5 6f1fb0eb9ff28476a721ce4a0832f5a7
BLAKE2b-256 fcd3c2165b27405972797d57a14f50db047171d05f3066619b47c580b855ff79

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.3-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 171.6 kB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for threaded-3.0.3-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 8a3febdaad853f5877b21f431c84e3d3ca967312c03d1c54d98901d78690697a
MD5 752931c1ef82eab0fa171d81f1945b51
BLAKE2b-256 ae6f1d4ed46d51137f37bd719f455b9a9b57a730d5bb516f5255ed99f23ab737

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.3-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 145.8 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for threaded-3.0.3-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 6924d2459d725bff5209f49e33bf49c9aefabc02ebdbbb64206fa63d6f4d9549
MD5 d4511b5f17c074ba7c424b0b5f7447c4
BLAKE2b-256 55e8ca21ab18e3e19d48d17e5d257e85a66c21ecf5ba5863d9870f86c9ef1cf0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.3-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 657.3 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 15cff3b86f18f145eab79b75fa4f04324e5ee72c0aa5c3a923bcd04838bdb4a7
MD5 34b86172b75bad131ac87efdbf81d9a6
BLAKE2b-256 019181a026721c7469e16206d2079582b58b42041f26476448fca1d5464f78d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.3-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 604.1 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.3-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 154f2a9320915dc9363878dbd62dfa0adf705b3a2fcdc9ae8dd56ef99be6219e
MD5 7969c9f2faa68e3d5c5530fb810677d4
BLAKE2b-256 98bcd205ed21aecb183a93316d6ff1d1bfa573d213ee55baacdabf706439bdc0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.3-cp34-cp34m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 621.5 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0317e9f90c4370e0a08aafdf4cabaa8ededdee457592f90c5d1dfcdd9c937462
MD5 0becc5daaa29a5aa8a046e5a6180fd86
BLAKE2b-256 bf532a5d57ac28ab2b85818a8f082b3af82409b76271da0a1c1c6600914f51f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.3-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 604.6 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for threaded-3.0.3-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d39462041ef3ca1980af2d03216cad23374ff333ecce729bee6a3a8a9cfb3bec
MD5 9ac052b62ee84ae5a122a5eae1e2d2b6
BLAKE2b-256 9f3ea471c252f7609cdc79fb8b99952736504a904ba1a6cea92fae482c589bdc

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