python - How to write a method decorated with @defer.inlineCallbacks that may or may not yield? -


my method of following form:

@defer.inlinecallbacks def myasyncmethod():     if somecondition:         yield anotherasyncmethod() 

the problem if somecondition not true no yield happens , becomes synchronous function. decorator causes error.

right now, doing yield 1 @ end of it. right thing here? of course do:

d = deferred() d.callback(0) yield d 

but don't see how different

edit: meant here if try yield myasyncmethod() generate exception. dont want have handle exception. 1 way avoid yield @ end of myasyncmethod(), there no-hacky way of doing this. common practice here?

the premise incorrect. consider:

>>> def foo(): ...     if false: ...             yield ...  >>> foo() <generator object foo @ 0x7f579cc4ccd0> >>>  

here, yield statement never evaluated. doesn't stop foo returning generator, though.

this means can decorate such function inlinecallbacks without problem.

>>> @inlinecallbacks ... def foo(): ...     if false: ...             yield ...  >>> foo() <deferred @ 0x7f08328e3ef0  current result: none> >>>  

since generator has no elements, deferred has none result, can see here.

further, means can call such function , yield inlinecallbacks-decorated function without problems:

>>> @inlinecallbacks ... def bar(): ...     print "foo result is:", (yield foo()) ...  >>> bar() foo result is: none <deferred @ 0x7f08328e3ef0  current result: none> >>>  

here see bar executed, yielded result of calling foo, printed out result of deferred, , completed own deferred (which has none result).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -