我正在理解龙卷风的协程,所以让我们保持一切简单,粘贴的代码越多越好。龙卷风协程 - 自定义函数
我想要的是使我的自制函数异步。
我可以在文档中找到的所有示例均属于相同的“隐藏”部分:AsyncHTTPClient。我不打算做一个HTTP调用。所以请不要给我那个班的例子。我有兴趣从头开始创造一些东西。我试过所有的可能性Tornado coroutine
现在我已经测试了bash睡眠。下面是代码:
import tornado.web
import tornado.httpserver
import tornado.gen
import tornado.concurrent
import subprocess
import os
@tornado.gen.coroutine
def letswait():
fut = tornado.concurrent.Future()
subprocess.check_output(["sleep", "5"])
fut.set_result(42)
return fut
class TestHandler1(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
value = yield letswait()
self.render("test.html", num=value)
class TestHandler2(tornado.web.RequestHandler):
def get(self):
self.render("test.html", num=66)
class Application(tornado.web.Application):
def __init__(self):
DIRNAME = os.path.dirname(__file__)
STATIC_PATH = os.path.join(DIRNAME, '../static')
TEMPLATE_PATH = os.path.join(DIRNAME, '../template')
sets = {
"template_path":TEMPLATE_PATH,
"static_path":STATIC_PATH,
"debug":True,
}
tornado.web.Application.__init__(self, [
(r"/test1", TestHandler1),
(r"/test2", TestHandler2),
], **sets)
def main():
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(8888)
print "Let s start"
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
但是如果我访问test1的话,我需要等待调用返回之前,我可以访问test2的。根据我的理解,我需要使用gen.sleep(5)
。但这只是一个例子。让我们说,而不是在bash上运行sleep 5
,我正在运行ssh somewhere 'do_something'
,这需要一些时间来运行。
我被告知“这个函数不是异步的”。所以我的问题是我如何使自定义函数异步?
编辑:搜索了一下后,我看到有在这里使用的tornado.process https://gist.github.com/FZambia/5756470。但是我的子进程来自第三方,所以它不是我可以覆盖的东西。所以我的问题是,如何将第三方库与gen.coroutine系统集成?
SOLUTION:多亏了下面的评论我已经有了一个解决方案:
import tornado.web
import tornado.httpserver
import tornado.gen
import tornado.concurrent
import subprocess
import os
from concurrent import futures
# Create a threadpool, and this can be shared around different python files
# which will not re-create 10 threadpools when we call it.
# we can a handful of executors for running synchronous tasks
# Create a 10 thread threadpool that we can use to call any synchronous/blocking functions
executor = futures.ThreadPoolExecutor(10)
def letswait():
result_future = tornado.concurrent.Future()
subprocess.check_output(["sleep", "5"])
result_future.set_result(42)
return result_future
class TestHandler1(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
value = yield executor.submit(letswait)
self.render("test.html", num=value)
class TestHandler2(tornado.web.RequestHandler):
def get(self):
self.render("test.html", num=66)
class Application(tornado.web.Application):
def __init__(self):
DIRNAME = os.path.dirname(__file__)
STATIC_PATH = os.path.join(DIRNAME, '../static')
TEMPLATE_PATH = os.path.join(DIRNAME, '../template')
sets = {
"template_path":TEMPLATE_PATH,
"static_path":STATIC_PATH,
"debug":True,
}
tornado.web.Application.__init__(self, [
(r"/test1", TestHandler1),
(r"/test2", TestHandler2),
], **sets)
def main():
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(8888)
print "Let s start"
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
你可以尝试删除.result()?那是我的错误。收益率应该自动得到结果。有可能你正在等待结果(),并且它会变成阻塞状态。 – user1157751