Skip to main content

简化和控制(运行和停止)python 线程(worker)

项目描述

蟒蛇工人

下载

安装

pip install python-worker

描述

通过使用装饰器或通过函数直接简化线程声明的包。它还允许您从任何层停止正在运行的线程(工作者)

由 Danangjoyo (c) 2020 开发

存储库


基本指南

@worker运行后将函数定义为线程对象

import time
from worker import worker

@worker
def go(n, sleepDur):
  for i in range(n):
    time.sleep(sleepDur)
  print('done')

go(100, 0.1)

该函数go将作为线程运行


异步指南

好吧,如果你有一个协程函数,你可以async_worker使用

import asyncio
from worker import async_worker

@async_worker
async def go():
    print("this is inside coroutine!")
    for i in range(10):
        time.sleep(0.5)
        print(i)
    print("done!")
    return "result!"

go_worker = asyncio.run(go())

杀死/停止/中止正在运行的工人

您可以中止一些工作人员、所有工作人员甚至所有线程..

中止特定的工人

import time
from worker import worker, abort_worker

@worker
def go4(n=10):
    for i in range(n):
        time.sleep(1)

go4_worker = go4(10)
time.sleep(3)
abort_worker(go4_worker)

或者只是从实例中止它

go4_worker.abort()

中止所有工作线程(这只中止工作线程)

from worker import abort_all_worker

abort_all_worker()

中止所有线程(它将中止所有工作线程和非工作线程)

from worker import abort_all_thread

abort_all_thread()

运行未定义的@worker函数

import time
from worker import run_as_Worker

def go(n):
    ...

go_worker = run_as_Worker(target=go, args=(10,))

获取返回值

如何获得线程函数的返回?

@worker
def go(n):
    time.sleep(n)
    return "done"

go_worker = go(10)

# this will await the worker to finished and return the value

go_result = go_worker.await

# You can also use this if it's finished, dont have to await

go_result = go_worker.ret

检查/监控所有工人

from worker import ThreadWorkerManager

## all created workers
ThreadWorkerManager.list()

## All active/running workers only
ThreadWorkerManager.list(active_only=True)

它将返回信息

>>> ThreadWorkerManager.list()
==============================================================
ID   |Name                |Active|Address        | WorkTime (s)   
==============================================================
0    |worker              |True  |0x7fdf1a977af0 | 4.97           
1    |worker1             |True  |0x7fdf1a73d640 | 4.07           
2    |worker2             |True  |0x7fdf1a73d9d0 | 3.83           
3    |worker3             |True  |0x7fdf1a73dd00 | 3.62           
4    |worker4             |True  |0x7fdf1a74b070 | 3.38           
==============================================================

Python 交互式 Shell - 键盘中断 (CTRL+C)

当您在交互模式下运行脚本时

python -i myScript.py

您可以添加一个带有键盘中断的中止处理程序来中止您的线程。

在 myScript.py 中

ThreadWorkerManager.enableKeyboardInterrupt()允许您中止正在运行的工人。

from worker import worker, ThreadWorkerManager


# enabling abort handler for worker into keyboard interrupt (CTRL+C)

ThreadWorkerManager.enableKeyboardInterrupt()

您还可以激活通过按 CTRL+Z 触发的退出线程。这还为键盘中断 (CTRL+C) 添加了一个用于 worker 的中止处理程序。

ThreadWorkerManager.disableKeyboardInterrupt(enable_exit_thread=True)

禁用工作程序的中止处理程序进入键盘中断 (CTRL+C)。

ThreadWorkerManager.disableKeyboardInterrupt()

检查处理程序状态。

ThreadWorkerManager.keyboard_interrupt_handler_status

您还可以选择允许在键盘中断时中止哪些工作人员

在 myScript.py 中

from worker import worker, ThreadWorkerManager

@worker("Uninterrupted", on_abort=lambda: print("ITS GREAT"), keyboard_interrupt=False)
def go_not_interrupted():
  i = 0
  while i < 1e3/2:
    i += 10
    print(i,"go_not_interrupted")
    time.sleep(0.001)
  return i

@worker("Interrupted", on_abort=lambda: print("ITS GREAT"), keyboard_interrupt=True)
def go_interrupted():
  i = 0
  while i < 1e3/2:
    i += 10
    print(i,"go_interrupted")
    time.sleep(0.001)
  return i

ThreadWorkerManagerManager.enableKeyboardInterrupt()
go_not_interrupted()
go_interrupted()

在您的终端中运行

python -i myScript.py

在进程运行时按 CTRL+C 并查看结果。


变更日志

  • v1.8:
    • 重构代码
    • 灵活worker申报
  • v1.9:
    • @async_worker使用装饰器为协程功能添加了异步工作器
  • v1.10:
    • overloadworker和添加了类型提示async_worker
    • restart为工人添加了功能
  • 未来:
    • 发展中process的工人

项目详情


下载文件

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

源分布

python-worker-1.10.1.tar.gz (8.9 kB 查看哈希)

已上传 source

内置分布

python_worker-1.10.1-py3-none-any.whl (8.9 kB 查看哈希

已上传 py3