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 2.7
Python 3.4
Python 3.5
Python 3.6
Python 3.7
PyPy
PyPy3 3.5+
Jyton 2.7

Decorators:

  • ThreadPooled - native concurrent.futures.ThreadPool usage on Python 3 and it’s backport on Python 2.7.

  • 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 Python 3 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)

Python 2.7 usage:

@threaded.ThreadPooled
def func():
    pass

concurrent.futures.wait([func()])

Python 3.3+ usage:

@threaded.ThreadPooled
def func():
    pass

concurrent.futures.wait([func()])

Python 3.3+ 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.3+ 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-1.0.6.tar.gz (18.9 kB view details)

Uploaded Source

Built Distributions

threaded-1.0.6-py2-none-any.whl (20.6 kB view details)

Uploaded Python 2

threaded-1.0.6-cp37-cp37m-manylinux1_x86_64.whl (715.4 kB view details)

Uploaded CPython 3.7m

threaded-1.0.6-cp37-cp37m-manylinux1_i686.whl (675.8 kB view details)

Uploaded CPython 3.7m

threaded-1.0.6-cp36-cp36m-manylinux1_x86_64.whl (715.1 kB view details)

Uploaded CPython 3.6m

threaded-1.0.6-cp36-cp36m-manylinux1_i686.whl (674.4 kB view details)

Uploaded CPython 3.6m

threaded-1.0.6-cp35-cp35m-manylinux1_x86_64.whl (696.7 kB view details)

Uploaded CPython 3.5m

threaded-1.0.6-cp35-cp35m-manylinux1_i686.whl (658.0 kB view details)

Uploaded CPython 3.5m

threaded-1.0.6-cp34-cp34m-manylinux1_x86_64.whl (712.3 kB view details)

Uploaded CPython 3.4m

threaded-1.0.6-cp34-cp34m-manylinux1_i686.whl (670.8 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

  • Download URL: threaded-1.0.6.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for threaded-1.0.6.tar.gz
Algorithm Hash digest
SHA256 4dc8e830fb61c398ec3f66c4acc3d50ebd9c967d4c40922fc19234c930ba3d01
MD5 440b74d63035759ccb20aa42f526f634
BLAKE2b-256 641631e6e8b2f586731c68e20fde3b0d543c519ce1c288cc9889ba4c8fa96a53

See more details on using hashes here.

File details

Details for the file threaded-1.0.6-py2-none-any.whl.

File metadata

File hashes

Hashes for threaded-1.0.6-py2-none-any.whl
Algorithm Hash digest
SHA256 c15d6a45c3d9056b3e10f0d09a62360ad1a7ab0ee392846ccf37687c36aa894c
MD5 3b9f40704d67f828f6bc7a996db854e7
BLAKE2b-256 8cbe1fd652cf5d1e9409a68da7b2bd064a47dfebb9552b5bdcae4400033ac7fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9861085b0e5219d3da1d7243100f3cebffbb09012a1a2ad639ec79f46f0acd2b
MD5 656c449fc18e11b9f168d7afb5bbe18e
BLAKE2b-256 c2eacb3c73d9a7660f93399e26b58ca3d011a9ffe84a66ce47a443bbc948ac1a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6e9398d3f9e1b4b90477ad25eea467bc2251625a44efc703a37bfbe877a9680b
MD5 bc11decd233dc683ad9f792c23f40cdf
BLAKE2b-256 48603928b7c76b69a73d92ace1ba2d341049466f2ba1336731cd12ea99e4f463

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2825de5daaf774a3f91e899b01a79e1680c453889194cacc5c86d4cfa16c9e1c
MD5 1859f373392f4d5bd8cba27844099787
BLAKE2b-256 b4f550ae9fd3a119b512a9345ee9d80ab8bf7c1a5a37275c33cdf207284f6d38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 be189dfe38b86b271f12b2e43cd151a24974251f33e52968d23439976ce26658
MD5 526e4048d3da32147f4ec1f40a202920
BLAKE2b-256 0e4b641b412a30a90889baea970411d387637c10a10e52ffe0c9ad5315b4b4a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 120f87e75b44f97e3b69658f7b8ce1b691c6ccb204acedca8a7dba1563fb6b6a
MD5 0c8490b736955bc8558e5d3d98fb3c07
BLAKE2b-256 f3c6195089e0b4b4a7ebd37bbb75ae46862793a8ac003dcbf4d15460701ab3db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b44c8caaf07ebe08186f6f64cc76b3bd23a6b197ef515dd6f0ce9793daa63d17
MD5 457faaf0613c1cfd0003f67938b67ff4
BLAKE2b-256 d5b024bc11d3ee97702d7fb7a26cf1e1f829624aa080ed1ca57f461db96963a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1c73e9e45157c97d87fef5dd6b0d630e3c444d8b5c77b5332e3edcddd59b7624
MD5 cb3cf2aab7660b72add6321982800265
BLAKE2b-256 da4e9ff69e984f05fbb7064061c67dc56adebe027c6c30a88f0630728acafa18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.6-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e3ca1a218fa18a97b4ab3c35c16fe9e23ee0819054ca94678332e86bdb6186c0
MD5 fa2920e288fb7bc72b495c888f38360f
BLAKE2b-256 ee3d1669cfcc56f4c128ef1511c5e7a0d2ece9c7182f51c36041db592bdba3c5

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