java炫酷龙卷风源码,使用龙卷风和扭曲的同时

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

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

解决方案

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:

obj is a Deferred in which case control is returned to the reactor until that Deferred fires.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值