from tornado import web, gen
import tornado, time
class CoroutineFactorialHandler(web.RequestHandler):
@web.asynchronous
@gen.coroutine
def get(self, n, *args, **kwargs):
n = int(n)
def callbacker(iterator, callback):
try:
value = next(iterator)
except StopIteration:
value = StopIteration
callback(value)
def factorial(n):
x = 1
for i in range(1, n+1):
x *= i
yield
yield x
iterator = factorial(n)
t = time.time()
self.set_header("Content-Type", "text/plain")
while True:
response = yield gen.Task(callbacker, iterator)
#log.debug("response: %r" %response)
if response is StopIteration:
break
elif response:
self.write("took : %f sec" %(time.time() - t))
self.write("\n")
self.write("f(%d) = %d" %(n, response))
self.finish()
application = tornado.web.Application([
(r"^/coroutine/factorial/(?P<n>\d+)", CoroutineFactorialHandler),
#http://localhost:8888/coroutine/factorial/<int:n>
])
if __name__ == "__main__":
application.listen(8888)
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.start()
上面拉出的 21 行是简单的阶乘计算器。它以生成器的方式循环 N 次。
问题是,当这段代码执行时,它会阻塞整个龙卷风。
我想要实现的是为龙卷风编写一些帮助程序,将生成器视为协程,因此可以以异步方式服务请求。(我已阅读Using a simple python generator as a co-routine in a Tornado async handler?)
为什么简单的增加和乘以 n 循环会阻止整个龙卷风?
编辑:我编辑了代码以包含整个应用程序,您可以运行和测试它。我在 python 2.7 上运行龙卷风 3.1.1