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

Uploaded Source

Built Distributions

threaded-1.0.4-py2-none-any.whl (20.9 kB view details)

Uploaded Python 2

threaded-1.0.4-cp36-cp36m-manylinux1_x86_64.whl (737.3 kB view details)

Uploaded CPython 3.6m

threaded-1.0.4-cp36-cp36m-manylinux1_i686.whl (700.1 kB view details)

Uploaded CPython 3.6m

threaded-1.0.4-cp35-cp35m-manylinux1_x86_64.whl (719.0 kB view details)

Uploaded CPython 3.5m

threaded-1.0.4-cp35-cp35m-manylinux1_i686.whl (683.4 kB view details)

Uploaded CPython 3.5m

threaded-1.0.4-cp34-cp34m-manylinux1_x86_64.whl (734.6 kB view details)

Uploaded CPython 3.4m

threaded-1.0.4-cp34-cp34m-manylinux1_i686.whl (696.8 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for threaded-1.0.4.tar.gz
Algorithm Hash digest
SHA256 fe1583a6e2b3bd2560d3321754ac43630842b6ecd71c6b009af9b69aa4d2a42d
MD5 9f5cfbb3fcd9ff86a3a6c4074876848a
BLAKE2b-256 56ac8d331d06dc8d12a5b1444c786796e43d8aa39928dadafce94df03674ff04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.4-py2-none-any.whl
Algorithm Hash digest
SHA256 9d1ca34285013e1783899a17eb687257fda67bdda5379951021827b416c3535e
MD5 404a49e028c766e6790117428e07a2b5
BLAKE2b-256 a8742dbba3388608959ae611b947bbd174961befaa540a196f9e0db837267e2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c99e448d24254de7a76a65213da77c85c8f941920761e6ebaac3e227c6f6eef1
MD5 37fb160253601fda6d7cd60f1031eb2e
BLAKE2b-256 db214284ed18acc62cf12dfa4df354e1b0f9de3ff35b26352a4a6a30dfde45d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.4-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0c358558f3ea3737eb03adb766e1ba28af9ca6ab683716191deab8995ce6f140
MD5 6f86d80f1434caad4b2d85d68901ef95
BLAKE2b-256 678e1e5fbb1d464cf0c37cfe3be8db04bb7dd4f0da53f2043606d69198af7532

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 949c6a86d832c9991f50436cfb56f48df389940f05d389223febee7a8c31fca3
MD5 7834e4a6b5777ae073d3344f5457d881
BLAKE2b-256 f90512088b8f5a2f8d5021b067c60a89ce10e22605f28986fd4eedc9ea8fa66c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.4-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8e2f2bbb55165540849b8c6fcd4fe1dc934551b21a3d1a96642973355dcae7d4
MD5 5737de0a29cb34012090b3aa4757d126
BLAKE2b-256 509f44d2857502df5b8bc0064630ff0ab3872fe9d76a72ad3523f6047ec738cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.4-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 22e18a6bf271726461d87231e4874418522eb565ed4783e840b4bad8e667985d
MD5 5a42d18dc4f64179611e80cac0728357
BLAKE2b-256 60b434b5c064dccb5161d73187906733029dce22e200d415e1125cf6edd8cd4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.4-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 ddc776c6fcf6088d296087dcb33be5fe59537c8c31cabfb0df73f3f2d4d0096b
MD5 15508837597a265f4d3932072bcda547
BLAKE2b-256 91cc147db5b5afe6aaefc9796cb84fa923ae5c2d9f7f688cf42529a8a3db3543

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