time.sleep、Flask 和 I/O 等待 [英] time.sleep, Flask and I/O wait

查看:44
本文介绍了time.sleep、Flask 和 I/O 等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用time.sleep()时,Flask请求会被阻塞吗?

When using time.sleep(), will a Flask request be blocked?

我的一个 Flask 端点启动了一个长处理子任务,在某些情况下,不是异步执行工作,而是等待任务完成并在同一个请求中返回结果.

One of my Flask endpoint launches a long processing subtask, and in some cases, instead of doing the work asynchronously, it is possible to wait for the completion of the task and return the result in the same request.

在这种情况下,我的 Flask 应用程序启动进程,然后等待它完成,然后返回结果.我的问题是,在调用类似(简化)的内容时:

In this case, my Flask app starts the process, then waits for it to complete before returning the result. My issue here, is that while calling something like (simplified):

while True:
    if process_is_done():
        break

    time.sleep(1)

Flask 会阻止该请求直到它完成,还是会允许其他请求在此期间到来?

Will Flask will block that request until it is done, or will it allow for other requests to come in the meantime?

推荐答案

是的,该请求已被完全阻止.time.sleep() 不通知任何睡眠,它只是在持续时间内空闲"CPU.

Yes, that request is entirely blocked. time.sleep() does not inform anything of the sleep, it just 'idles' the CPU for the duration.

Flask 本身不是异步的,它没有暂停请求处理程序并为其他请求提供更多时间的概念.一个好的WSGI服务器会使用线程和/或多个工作进程来实现并发,但是这个请求还是被阻塞了,占用了CPU时间.

Flask is itself not asynchronous, it has no concept of putting a request handler on hold and giving other requests more time. A good WSGI server will use threads and or multiple worker processes to achieve concurrency, but this one request is blocked and taking up CPU time all the same.

这篇关于time.sleep、Flask 和 I/O 等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆