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

Uploaded Source

Built Distributions

threaded-4.0.1-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

threaded-4.0.1-cp37-cp37m-win_amd64.whl (187.4 kB view details)

Uploaded CPython 3.7m Windows x86-64

threaded-4.0.1-cp37-cp37m-win32.whl (160.1 kB view details)

Uploaded CPython 3.7m Windows x86

threaded-4.0.1-cp37-cp37m-manylinux1_x86_64.whl (686.7 kB view details)

Uploaded CPython 3.7m

threaded-4.0.1-cp37-cp37m-manylinux1_i686.whl (636.1 kB view details)

Uploaded CPython 3.7m

threaded-4.0.1-cp36-cp36m-win_amd64.whl (187.7 kB view details)

Uploaded CPython 3.6m Windows x86-64

threaded-4.0.1-cp36-cp36m-win32.whl (160.1 kB view details)

Uploaded CPython 3.6m Windows x86

threaded-4.0.1-cp36-cp36m-manylinux1_x86_64.whl (686.1 kB view details)

Uploaded CPython 3.6m

threaded-4.0.1-cp36-cp36m-manylinux1_i686.whl (634.1 kB view details)

Uploaded CPython 3.6m

File details

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

File metadata

  • Download URL: threaded-4.0.1.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.1.tar.gz
Algorithm Hash digest
SHA256 12b6bb962372013aeb83fe0dde84bc3c22b941a5ff569a9170763cbbbe66081f
MD5 7274b557ce69a39cadef0cafc23ec735
BLAKE2b-256 07c3ca0c9e8285c8b5c333c44ac05c2d84b7b860490128a85ac698d3b305c214

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.6 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce9249968d12cf8411b82021306dcdb4613ee71cf0475f22623437ee5d1e264b
MD5 b1b6bb874887b6fd0e0db8c0e5d9a605
BLAKE2b-256 a310d7e3cd0155dbd1f310986f6fd001753d39802ded88d7a4ed7bdf383a75f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 187.4 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for threaded-4.0.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 95e0d94b7ca82bc48072fafedf7b2e63822a28f71608571facfa4b585dcdbbcc
MD5 d952cf22530c04f500786bd80e411201
BLAKE2b-256 830eb5a52badf65fd58aa2cc8d990697fd4603f326038f95639dedd0a7289954

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 160.1 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for threaded-4.0.1-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 53fb5085b31167a1c633c87b05628b1d329de7e7d5c3aa67d46f67680f2ebb44
MD5 05086af34ea4b8190001a0db38952cdb
BLAKE2b-256 f77b23b3a6d73e9ddf707828eb5f856752283c951bc25d8e421c973f35e4d146

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 686.7 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 15b5fe4fe40ccc90cc31f6d502ba9c1ab1c479bdc49c122b690845e1a78fd118
MD5 74d4af54d482986771ef15c8f61a1eba
BLAKE2b-256 69d5c3cf13a3c3545dfd67a84afaf70c1b07b07a8840dd3e95ebf99d0b9e0286

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 636.1 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.1-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6d00fd390b825764cb7df14c99d26b6530c633aaa64421c632f5f4fa94b2d91d
MD5 e14a86552de2f6a54db697361251c18d
BLAKE2b-256 c9e613b2f68d436b3cc46972f00ace5018e6538bb17317494af984bc10e3cc0f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 187.7 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

File hashes

Hashes for threaded-4.0.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 bbdbb8ee8a5fa600d58d14895156ccfb362cd0ea2e3da2ec650511b212b77a69
MD5 ac59c70f4cceb1bf4a117d3c2704ad50
BLAKE2b-256 0ffe216e53dd6522c98581e4e28983c98f0f33791c73ae2e32bad2b81389d538

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 160.1 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

File hashes

Hashes for threaded-4.0.1-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 22200d12abebe5d6fedb5bd941f25d47e8269a899c636a497fa37f925312f9e7
MD5 20639a2599bfb693c2b4587e3a94fd71
BLAKE2b-256 22d593e2183cdbb5dc3728e79bb5e81061c1a9df03ac35cbf57cbf6188dc74f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 686.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 14a8647bc8ee16112225109a63b997d95bd3874d7d0c013c4e79341a215fc965
MD5 53bc8d6ee27638ad97304061a5518071
BLAKE2b-256 5c2c398362c66325cd2baac7feb5ec1d34ff8fdad43cd253d1949fc49f9be33f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: threaded-4.0.1-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 634.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for threaded-4.0.1-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9ce8403ae2ca69e789481a485ba807f6e5bb79129e5b6c56ea07b58f697777b0
MD5 1ebd1839001e5ffaa404c559bbe36c5c
BLAKE2b-256 5c9beee304cfdae88d801907677bf863910aa287a51fd4c841deda236a00538e

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