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

Uploaded Source

Built Distributions

threaded-1.0.0-cp36-cp36m-manylinux1_x86_64.whl (708.3 kB view details)

Uploaded CPython 3.6m

threaded-1.0.0-cp36-cp36m-manylinux1_i686.whl (665.3 kB view details)

Uploaded CPython 3.6m

threaded-1.0.0-cp35-cp35m-manylinux1_x86_64.whl (689.2 kB view details)

Uploaded CPython 3.5m

threaded-1.0.0-cp35-cp35m-manylinux1_i686.whl (648.8 kB view details)

Uploaded CPython 3.5m

threaded-1.0.0-cp34-cp34m-manylinux1_x86_64.whl (705.4 kB view details)

Uploaded CPython 3.4m

threaded-1.0.0-cp34-cp34m-manylinux1_i686.whl (661.7 kB view details)

Uploaded CPython 3.4m

File details

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

File metadata

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

File hashes

Hashes for threaded-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2725b2b262f4041a0f53e1816af6da31e6f96015d7141f3e9b8dd200b08e93e3
MD5 fbc9cf196c8f773f87d7564ac58eea8d
BLAKE2b-256 8f8ec2521d344385617c37c4503e5fa7e7fdb2e0e6ce9b66297d96981e5de80e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 09287405d3510362c0da18f13f5ea38e95d7d385203aed8060a07fef8bf5378c
MD5 4daac6588a430704f9bd2b3058d54001
BLAKE2b-256 e1fbbb708e56f11c6216d55586abec8ddbd77d4aea0eea1b051728321061b56e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 3938d747db0a7c8be9d865a47149061f7ae6476cc48c855120294473a5dc9a0d
MD5 52f9fe2498966e68c80d439452cdf5b1
BLAKE2b-256 5237351f115998f31c7b2aa8a99ea6979b8e5149acbcb01adc8811d8efe2df03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8ad497bc9a5c28f8549e35f751f5684b6756ee623cbf9146fd7d186ab76ba23a
MD5 9b12dcff16e1933c321710ce55e5b2a6
BLAKE2b-256 0879f70e2e90192538047c4dce84ce633a19c75da927e6bd8009e1ce65420180

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 acbf9b98ba84b4c539fef7fe3e9b00d3a6b2ff34b476192bd3bab891f4b307f6
MD5 b9dc59f33041b31968ddd2653c34a302
BLAKE2b-256 a723369fa01e556cbb9a3dea50f84188030192abcac4965d418c4817579e904b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b297a40a2a5dfba9a413ad6f989ba6d81a79778d5880ff0cdc742743c879e6ec
MD5 aca7db9c1ef7e3b8a9b25f982d1706ff
BLAKE2b-256 20d863905b0c4c4486afada456d338ac7d46381061372615f8aa2e0e0695efe7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for threaded-1.0.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 1cd5f1cb42634cf39f82db64c2b88143c49a77db91aba60edda8addc9125cb5c
MD5 25d28a80daded707651788b48b03a4b5
BLAKE2b-256 572743722b421b23f4426d4179521d53c10ac8e380d83d10c575cb75808fa33c

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