【问题标题】:Python Quart Unable to shutdown background taskPython Quart 无法关闭后台任务
【发布时间】:2021-01-06 20:35:59
【问题描述】:

我正在开发一个 Python 应用程序,但我正在从 Flask 迁移到 Quart。应用程序需要一个在应用程序运行时不断运行的后台任务。

当我尝试使用 control-c 停止进程时,线程不会完全关闭,而是处于关闭例程的 while 循环中。

    while not self._master_thread_class.shutdown_completed:
        if not pro:
            print('[DEBUG] Thread is not complete')
            pro = True

我关注了this Stackoverflow 问题,但我不知道如何干净地关闭后台线程,所以我想请解释一下,因为 Quart 文档似乎缺少一点。

MasterThread 类:

import asyncio

class MasterThread:

    def __init__(self, shutdown_requested_event):
        self._shutdown_completed = False
        self._shutdown_requested_event = shutdown_requested_event
        self._shutdown_requested = False

    def __del__(self):
        print('Thread was deleted')

    def run(self, loop) -> None:
        asyncio.set_event_loop(loop)
        loop.run_until_complete(self._async_entrypoint())

    @asyncio.coroutine
    def _async_entrypoint(self) -> None:
        while not self. _shutdown_requested and \
            not self._shutdown_requested_event.isSet():
            #print('_main_loop()')
            pass

            if self._shutdown_requested_event.wait(0.1):
                self. _shutdown_requested = True

        print('[DEBUG] thread has completed....')
        self._shutdown_completed = True

    def _main_loop(self) -> None:
        print('_main_loop()')

主应用模块:

import asyncio
import threading
from quart import Quart
from workthr import MasterThread

app = Quart(__name__)

class Service:

    def __init__(self):
        self._shutdown_thread_event = threading.Event()
        self._master_thread = MasterThread(self._shutdown_thread_event)
        self._thread = None

    def __del__(self):
        self.stop()

    def start(self):
        loop = asyncio.get_event_loop()
        self._thread = threading.Thread(target=self._master_thread.run, args=(loop,))
        self._thread.start()
        return True

    def stop(self) -> None:
        print('[DEBUG] Stop signal caught...')
        self._shutdown_thread_event.set()
        while not self._master_thread.shutdown_completed:
            print('[DEBUG] Thread is not complete')

        print('[DEBUG] Thread has completed')

        self._shutdown()

    def _shutdown(self):
        print('Shutting down...')

service = Service()
service.start()

【问题讨论】:

    标签: python multithreading quart


    【解决方案1】:

    Quart 具有startup and shutdown 方法,允许在服务器开始服务之前启动某些东西,并在服务器完成服务时停止。如果您的后台任务主要是 IO 绑定,我建议您只使用协程函数而不是线程,

    async def background_task():
        while True:
            ...
    
    @app.before_serving
    async def startup():
        app.background_task = asyncio.ensure_future(background_task())
    
    @app.after_serving
    async def shutdown():
        app.background_task.cancel()  # Or use a variable in the while loop
    

    或者你可以对你的服务做同样的事情,

    @app.before_serving
    async def startup():
        service.start()
    
    @app.after_serving
    async def shutdown():
        service.stop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多