首页 > 解决方案 > 只有在第一个函数设置全局标志后才完成响应

问题描述

我有两个功能。首先执行检查服务器服务 url-checking。功能作为 sanic-scheduler 任务完成,每 2 秒运行一次。

@task(timedelta(seconds=2)) 
def check_data_status():
    ...
    isProcessed = True  

https://github.com/asmodius/sanic-scheduler

第二个是请求处理程序:

def new_answer(request):
    ...
    # we should wait and complete response only after isProcessed = True
    response answer

只有在第一个函数new_answer之后才应该完成响应。isProcessed = True

我怎么能做到。我正在使用 Sanic。

标签: pythonasynchronoussanic

解决方案


Sanic 正在使用与 asyncio 兼容的事件循环,因此您可以为此使用 asyncio 的内置同步原语:https ://docs.python.org/3/library/asyncio-sync.html 。事件似乎很适合您的情况。


推荐阅读