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

Uploaded Source

Built Distributions

threaded-3.0.1-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

threaded-3.0.1-cp35-cp35m-win_amd64.whl (171.5 kB view details)

Uploaded CPython 3.5m Windows x86-64

threaded-3.0.1-cp35-cp35m-win32.whl (145.7 kB view details)

Uploaded CPython 3.5m Windows x86

threaded-3.0.1-cp35-cp35m-manylinux1_x86_64.whl (656.6 kB view details)

Uploaded CPython 3.5m

threaded-3.0.1-cp35-cp35m-manylinux1_i686.whl (603.6 kB view details)

Uploaded CPython 3.5m

threaded-3.0.1-cp34-cp34m-manylinux1_x86_64.whl (621.0 kB view details)

Uploaded CPython 3.4m

threaded-3.0.1-cp34-cp34m-manylinux1_i686.whl (604.1 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for threaded-3.0.1.tar.gz
Algorithm Hash digest
SHA256 7a89f5d00c7a5048527151bfac1e674ae4d3a6cbebf13895a30cca48c6885e0a
MD5 6334a1549916a3d214ae0973680ba66e
BLAKE2b-256 b5f94d488fff32ec9bd5cd59674a0c5a181a01df5a7111c16e23368e4cdfce34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.4.4

File hashes

Hashes for threaded-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 42d51284229acfd2aa5b6f6c7e4fb9553d8d0d04a960b27788736ce1726c425b
MD5 fd6196794a73f2078dc9d8257bb6cb74
BLAKE2b-256 fa83f7d479ad479f7877e78eefcc965b715e2055600b7288df0c4ae6c4c10f26

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.1-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 171.5 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.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.4

File hashes

Hashes for threaded-3.0.1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 f89ab0cc08969c125b190718cb496aabcb76512beac6ba0269a68fc6e355e071
MD5 e72edb6dd9df4a270555c627c8684517
BLAKE2b-256 026b1a30637ab4b175dc7accf4fbc2fbff55e20f53da7980039bd8f4fae6906a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-3.0.1-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 145.7 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.4

File hashes

Hashes for threaded-3.0.1-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 8a5226974bcf5dc930d77d5d6a14ef185a46ac048feba2ca8c2f8e2cb18d369a
MD5 9cd9c892b693d0ba9564abe1ce4987a1
BLAKE2b-256 ff66d77cdbc518b13b98bae9e0fa4b96e62d4a604b01134437f5a3250bc0aeb0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for threaded-3.0.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 544c56f353eac8522825f7bf71ba6af8e113188bb3e0bf19c8077f4f76036638
MD5 8c3232b3da20e2b49158c3060f21f43f
BLAKE2b-256 5abd89af425e78044f33db0eb578e898823872385f331ffadaef2e294b7971d5

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for threaded-3.0.1-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8a84d93bd75ca74588e80005ff4d2c7e1238e7f5ba5de9de2c23d4eb3248ee45
MD5 6c1efe44dad5128c4513f08bf1208bbf
BLAKE2b-256 9df1ebb88b6dd9f5c4887f647b70898775c5dd2cba170ebd18985fdea9c37efe

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for threaded-3.0.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a59072f95dc8f3f8ccdb759610e6083645c8fc23fb8f63ed21eaab9bda4c5fea
MD5 e39a80bf2f1ace249c565565a0a4920b
BLAKE2b-256 61c5ab3e8921f11fee23b0ed424ff53ede3ad163aa57c4db437a8cd519ce829f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for threaded-3.0.1-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b6902c59ba32021024a46fd870d5bb405be75c538e3af809e3b4d0d521c31c61
MD5 e09fea59e050432551a0e84f925fc58a
BLAKE2b-256 30caeccb2d9144dd1e47950c96540259dc8a4b3774b74da7972128d88096233c

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