【发布时间】:2018-06-24 21:36:04
【问题描述】:
场景:首先,我需要将数据库中的状态更新为“待处理”,同时将状态返回给用户。然后子进程将在后台运行,这将需要 30 秒,因为我已将 time.sleep(30) 放入 dummy.py 中。之后,我必须将数据库中的状态更新为“已完成”。我正在尝试使用龙卷风制作非阻塞功能。
我的问题:我已经捕获了 Subprocess 是否通过使用 yield 完成。如果 yield result 为 0,我假设 Subprocess 已经完成。我知道我的逻辑有些不对劲。如果 Subprocess(Tornado) 以正确的方式完成,我如何捕获?
我当前的代码是:
class MainHandler(tornado.web.RequestHandler):
@coroutine
def get(self, id):
print ("TORNADO ALERT")
self.write("Pending")
#If ID in DB, UPDATE DB
#Update Status to Pending
self.flush()
res =yield self._work()
self.write(res)
@coroutine
def _work(self):
p = Subprocess(['python', 'dummy.py'])
f = Future()
p.set_exit_callback(f.set_result)
h = yield f
print (">>> ",h)
if h == 0:
print("DB Updated")
#Update Status to Completed
raise Return(" Completed ")
我的导入如下:
from tornado.concurrent import Future
from tornado.process import Subprocess
【问题讨论】:
-
能否在代码 sn-p 中添加导入(特别是
Future)? -
我已经为未来添加了导入
-
你的操作系统是什么? set_exit_callback won't work on Windows
-
我在 Ubuntu 上工作
标签: python-3.x subprocess tornado