使用龙卷风和扭曲的同时 [英] Using Tornado and Twisted at the same time

查看:202
本文介绍了使用龙卷风和扭曲的同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个奇怪的情况下,我在完全建立了龙卷风的系统中使用双绞线。
他们可以共享相同的IOLoop所以我知道他们可以一起工作。我的问题是我能安全地使用他们的合作例行装饰在相同的功能?例如:

I am in a weird situation where I have to use Twisted in a system built completely out of Tornado. They can share the same IOLoop so I know they can work together. My question is can I safely use their co-routine decorators in the same function? For example:

import tornado.platform.twisted
tornado.platform.twisted.install()

...

@gen.engine
@defer.inlineCallbacks
def get(self):

    ...

    a = yield gen.Task(getA) # tornado
    b = yield proxy.callRemote(getB) # twisted

    ...

    defer.returnValue(a + b) # twisted

他们在同一个工作IOLoop所以我想这应该是罚款。会有任何不可预见的后果?先谢谢了。

They do work on the same IOLoop so I am thinking this should be fine. Would there be any unforeseen consequences? Thanks in advance.

推荐答案

没有,这是行不通的。在你的情况 inlineCallbacks 直接缠在你的发电机和 gen.engine 被包裹外面。问题是, inlineCallbacks 不知道 gen.Task 任何东西,它会立即产生它(它没有办法它传递到一起的 gen.engine )。

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).

要详细说明:如果你屈服 OBJ inlineCallbacks -wrapped发生器内,有两种情况:

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


  1. OBJ 递延在这种情况下,控制返回到反应器中,直到递延火灾。

  2. OBJ 是别的东西,在这种情况下,它会立即发送回您的发电机。

  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

请参阅这里如何 inlineCallbacks 实施

什么是做到这一点的正确方法?尝试使用任何 inlineCallbacks gen.engine (但不能同时)。总结 gen.Task (或递延)插入到本土的形式外星人。我不熟悉的龙卷风,但也许<一个href=\"http://stackoverflow.com/questions/19396711/is-it-possible-to-use-tornados-gen-engine-and-gen-task-with-twisted\">this问题帮助。

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.

另外,写自己喜欢的装饰 inlineCallbacks 处理 gen.Task 以及

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

这篇关于使用龙卷风和扭曲的同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆