Using Tornado and Twisted at the same time

允我心安 提交于 2019-12-06 13:44:59
Max

No, this wouldn't work. In your case inlineCallbacks is wrapped directly around your generator and gen.engine is wrapped outside. The problem is that inlineCallbacks does not know anything about gen.Task and it will yield it immediately (it has no way of passing it along to gen.engine).

To elaborate: if you yield obj inside an inlineCallbacks-wrapped generator, two things can happen:

  1. obj is a Deferred in which case control is returned to the reactor until that Deferred fires.
  2. obj is something else, in which case it is immediately sent back into your generator.

In your case, the result would be:

a = yield gen.Task(getA) # continues right through
# a is of type gen.Task here
b = yield proxy.callRemote(getB) # waits for result of proxy.callRemote

See here for how inlineCallbacks is implemented.

What is the right way to do this? Try to use either inlineCallbacks or gen.engine (but not both). Wrap the alien gen.Task (or Deferred) into the "native" form. I am not familiar with Tornado but maybe this question helps.

Alternatively, write your own decorator like inlineCallbacks that handles gen.Task as well.

Looks like what you want is Cyclone, a web server framework for Python that implements the Tornado API as a Twisted protocol.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!