Skip to main content

Decorators for running functions in Thread/ThreadPool/IOLoop

Project description

threaded

https://github.com/python-useful-helpers/threaded/workflows/Python%20package/badge.svg 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.

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:

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()])

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. GitHub actions: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests.

  2. coveralls: is used for coverage display.

CD system

GitHub actions: 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.2.0.tar.gz (30.1 kB view hashes)

Uploaded Source

Built Distributions

threaded-4.2.0-py3-none-any.whl (25.4 kB view hashes)

Uploaded Python 3

threaded-4.2.0-cp312-cp312-win_amd64.whl (184.8 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

threaded-4.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

threaded-4.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

threaded-4.2.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

threaded-4.2.0-cp311-cp311-win_amd64.whl (185.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

threaded-4.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

threaded-4.2.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

threaded-4.2.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (1.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

threaded-4.2.0-cp310-cp310-win_amd64.whl (185.7 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

threaded-4.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (948.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

threaded-4.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (942.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

threaded-4.2.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (910.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

threaded-4.2.0-cp39-cp39-win_amd64.whl (188.1 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

threaded-4.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (959.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

threaded-4.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (953.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

threaded-4.2.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (921.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

threaded-4.2.0-cp38-cp38-win_amd64.whl (188.8 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

threaded-4.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (971.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

threaded-4.2.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (965.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

threaded-4.2.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (938.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

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