Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273768
  • 博文数量: 40
  • 博客积分: 1807
  • 博客等级: 上尉
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-03 15:42







分类: Python/Ruby

2011-01-20 22:48:10

先简单说一下python decorator:
  1. @A
  2. def f ():
  3.     …

  4. 这种形式是decorator不带参数的写法。最终 Python 会处理为:

  5. f = A(f)


  1. from twisted.internet import defer
  2. from twisted.web import client
  3. from twisted.internet import reactor
  4. import sys

  5. @defer.inlineCallbacks
  6. def enqueueTaskToDisplayURL(url):
  7.     try:
  8.         data = yield client.getPage(url)
  9.         print data
  10.     except Exception, e:
  11.         print >> sys.stderr, "Error:", e
  12.         return;
  13.     finally: reactor.stop()

  14. if __name__ == "__main__":
  15.     enqueueTaskToDisplayURL("")

enqueueTaskToDisplayURL = defer.inlineCallbacks(enqueueTaskToDisplayURL)





1)通过@defer.inlineCallbacks + yield, 帮助程序员将异步的代码写成了同步的方式。

2)Twisted's @inlineCallbacks decorator basically does this for you; whenever you yield a deferred (Twisted's representation of an ongoing operation), the rest of the generator is registered as the callback for when the operation is finished.



inlineCallbacks helps you write Deferred-using code that looks like a regular sequential function. This function uses features of Python 2.5 generators. If you need to be compatible with Python 2.4 or before, use the deferredGenerator function instead, which accomplishes the same thing, but with somewhat more boilerplate. For example:

@inlineCallBacks def thingummy(): thing = yield makeSomeRequestResultingInDeferred() print thing #the result! hoorj!

When you call anything that results in a Deferred, you can simply yield it; your generator will automatically be resumed when the Deferred's result is available. The generator will be sent the result of the Deferred with the 'send' method on generators, or if the result was a failure, 'throw'.

Your inlineCallbacks-enabled generator will return a Deferred object, which will result in the return value of the generator (or will fail with a failure object if your generator raises an unhandled exception). Note that you can't use return result to return a value; use returnValue(result) instead. Falling off the end of the generator, or simply using return will cause the Deferred to have a result of None.

The Deferred returned from your deferred generator may errback if your generator raised an exception:

@inlineCallbacks def thingummy(): thing = yield makeSomeRequestResultingInDeferred() if thing == 'I love Twisted': # will become the result of the Deferred returnValue('TWISTED IS GREAT!') else: # will trigger an errback raise Exception('DESTROY ALL LIFE')
阅读(2198) | 评论(0) | 转发(0) |