【发布时间】:2020-06-11 15:58:07
【问题描述】:
在请求完成之前,似乎模块级变量实际上并未“更改”。 我需要它立即更新,以便其他请求可以知道该工作正在处理中。
这是一个问题的例子:
import cherrypy
import time
@cherrypy.expose
class CherryPySleeps(object):
status = "not sleeping"
@cherrypy.tools.accept(media='text/plain')
def GET(self):
if CherryPySleeps.status == "not sleeping":
CherryPySleeps.status = "sleeping"
time.sleep(10)
CherryPySleeps.status = "not sleeping"
return "I finished sleeping"
else:
return "I am sleeping, shhh"
if __name__ == '__main__':
conf = {
'/': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.sessions.on': True,
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
}
}
cherrypy.quickstart(CherryPySleeps(), '/', conf)
当我打开 2 个浏览器选项卡时,在第一个页面请求页面,然后等待一秒钟,然后在第二个页面请求页面,我在每个选项卡上暂停 10 秒,然后“我已完成睡眠”。
我想要的是第二个标签快速响应,“我在睡觉,嘘”
【问题讨论】: