Skip to main content

Decorators for running functions in Thread/ThreadPool/IOLoop

Project description

threaded

https://travis-ci.org/penguinolog/threaded.svg?branch=master https://coveralls.io/repos/github/penguinolog/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/penguinolog/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
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. Test environments available:

pep8
py27
py34
py35
py36
pypy
pypy3
pylint

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

Uploaded Source

Built Distributions

threaded-0.9.0-cp36-cp36m-manylinux1_x86_64.whl (708.4 kB view details)

Uploaded CPython 3.6m

threaded-0.9.0-cp36-cp36m-manylinux1_i686.whl (665.4 kB view details)

Uploaded CPython 3.6m

threaded-0.9.0-cp35-cp35m-manylinux1_x86_64.whl (689.4 kB view details)

Uploaded CPython 3.5m

threaded-0.9.0-cp35-cp35m-manylinux1_i686.whl (648.9 kB view details)

Uploaded CPython 3.5m

threaded-0.9.0-cp34-cp34m-manylinux1_x86_64.whl (705.5 kB view details)

Uploaded CPython 3.4m

threaded-0.9.0-cp34-cp34m-manylinux1_i686.whl (661.8 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for threaded-0.9.0.tar.gz
Algorithm Hash digest
SHA256 be849713677a4cc99371afb20ba5337d59e9f4ec6b3f6d0a77409f3fbe2212a3
MD5 a8a6bd15e083cfb08c7ac27f28f04507
BLAKE2b-256 0071f83bb69ab9768703d6ad39ea037eed9c8224050d51cc6293dbbb1f0283d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-0.9.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c35f035e34be5ad6d945258a1e3fe0ed98d18e215bceb3e18fc39595704d6a4b
MD5 607f509ebd6ea254291b0fd9120bf932
BLAKE2b-256 cb4006b17b15dc349a829a5c753d6b5cc0ff5c819065d74c7fb981b6ae24b0d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-0.9.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 04f4edb390db8bda46678524deaba8bd6f076108833721e16c6ce67b87a885d6
MD5 2926174bac434b4138532131006a6a51
BLAKE2b-256 0a481536a955a822ece06fadc3924c7b1277479839e38deadbf67ed641e7ea52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-0.9.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bef9fc4fe003d33230481befe19b40b078dca3bfaadd0fcec6faed712df61b52
MD5 23b31a8cf06deb102b4ae249cdbe1f78
BLAKE2b-256 091207f1570ba9c249afcaf506a816b553dd01d35c4e2806e5407de709b22c82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-0.9.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 bdd80edd803320275793361248b057cf046d808c56a097e78b07aac5dd0fe7d4
MD5 f4736f276719b7f93b83115f32b7c94e
BLAKE2b-256 5907e0bce60f1f0661cac78444bc17b207ae165ff624fad6addbd50272f4a836

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-0.9.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c3eddbb9a0726d6b6fc820fda7354eec0845d3e37c5f2a11b90ab48682771a44
MD5 10748930969666f2ecdce62990dfd5b5
BLAKE2b-256 505137b543a758aacf3f535f4042c323a20324814580ab4172e6dfda38f32a2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-0.9.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 5a31ca8d3f1163665123d371cb5c1a6a55ce07d25dbb159807ca67e1803f4077
MD5 6d0fe4cba0fd65f681e00e8d49270f04
BLAKE2b-256 4b9ae54a1a079f782bd1a7a3bda085b170d2ca899c2aefbc4d195e7ec7dd4bc2

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