跳转到主要内容

用于在Thread/ThreadPool/IOLoop中运行函数的装饰器

项目描述

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是一组装饰器,用于在Python 3中将函数包装在

  • concurrent.futures.ThreadPool

  • threading.Thread

  • asyncio.Task中。

为什么?因为复制粘贴 loop.create_taskthreading.Threadthread_pool.submit 是无聊的,尤其是当目标函数仅以此方式使用时。

优点

装饰器

  • ThreadPooled - 本地 concurrent.futures.ThreadPool

  • threadpooledThreadPooled 的别名。

  • Threaded - 包装在 threading.Thread 中。

  • threadedThreaded 的别名。

  • AsyncIOTask - 包装在 asyncio.Task 中。使用与 ThreadPooled 相同的API。

  • asynciotaskAsyncIOTask 的别名。

用法

ThreadPooled

通常需要装饰器:在调用时将函数提交给ThreadPoolExecutor。

threaded.ThreadPooled.configure(max_workers=3)
@threaded.ThreadPooled
def func():
    pass

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

与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+与asyncio和从调用参数中提取loop的使用

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

在应用程序关闭期间,池可以被停止(尽管它将在有组件请求时自动重新创建)。

threaded.ThreadPooled.shutdown()

Threaded

经典的threading.Thread。适用于运行直到关闭并自动关闭的线程,没有返回值。

使用示例

@threaded.Threaded
def func(*args, **kwargs):
    pass

thread = func()
thread.start()
thread.join()

没有参数时,线程名称将使用模式:'Threaded: ' + func.__name__

可以通过相应参数覆盖名称。

@threaded.Threaded(name='Function in thread')
def func(*args, **kwargs):
    pass

线程可以自动成为守护进程。

@threaded.Threaded(daemon=True)
def func(*args, **kwargs):
    pass

此外,如果不需要在线程启动前进行任何其他操作,可以在返回之前自动启动。

@threaded.Threaded(started=True)
def func(*args, **kwargs):
    pass

AsyncIOTask

封装在asyncio.Task中。

与asyncio的使用

@threaded.AsyncIOTask
def func():
    pass

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait_for(func(), timeout))

直接提供事件循环。

loop = asyncio.get_event_loop()
@threaded.AsyncIOTask(loop_getter=loop)
def func():
    pass

loop.run_until_complete(asyncio.wait_for(func(), timeout))

从调用参数中提取loop的使用

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

测试

该包threaded的主要测试机制是使用tox。可以通过tox -l收集可用的环境。

CI系统

在并行使用几个CI系统进行代码检查。

  1. GitHub actions:用于检查:PEP8、pylint、bandit、安装可能性和单元测试。

  2. coveralls:用于显示覆盖率。

CD系统

GitHub actions:用于在PyPI上打包。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

threaded-4.2.0.tar.gz (30.1 kB 查看散列)

构建分布

threaded-4.2.0-py3-none-any.whl (25.4 kB 查看散列)

上传于 Python 3

threaded-4.2.0-cp312-cp312-win_amd64.whl (184.8 kB 查看哈希值)

上传于 CPython 3.12 Windows x86-64

threaded-4.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB 查看哈希值)

上传于 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 查看哈希值)

上传于 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 查看哈希值)

上传于 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 查看哈希值)

上传于 CPython 3.11 Windows x86-64

threaded-4.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB 查看哈希值)

上传于 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 查看哈希值)

上传于 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 查看哈希值)

上传于 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 查看哈希值)

上传于 CPython 3.10 Windows x86-64

threaded-4.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (948.1 kB 查看哈希值)

上传时间: 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 查看哈希值)

上传时间: 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 查看哈希值)

上传时间: 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 查看哈希值)

上传时间: CPython 3.9 Windows x86-64

threaded-4.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (959.2 kB 查看哈希值)

上传时间: 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 查看哈希值)

上传时间: 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 查看哈希值)

上传时间: 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 查看哈希值)

上传时间: CPython 3.8 Windows x86-64

threaded-4.2.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (971.9 kB 查看哈希值)

上传时间: 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 查看哈希值)

上传于 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 查看哈希值)

上传于 CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页面