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

全部博文(40)

文章存档

2011年(18)

2010年(20)

2009年(2)

我的朋友

分类: Python/Ruby

2011-01-20 23:26:36

在此摘抄一个一般defer和defer.inlineCallbacks的用法比较的例子:
  1. view sourceprint?from twisted.internet import reactor, defer

  2. from twisted.web.client import getPage

  3. import re

  4.    

  5. def lookup(country, search_term):

  6.     main_d = defer.Deferred()

  7.    

  8.     def first_step():

  9.         query = "%s/search?q=%s" % (country,search_term)

  10.         d = getPage(query)

  11.         d.addCallback(second_step, country)

  12.         d.addErrback(failure, country)

  13.    

  14.     def second_step(content, country):

  15.         m = re.search('
    http://[^"]+)"',

  16.                       content, re.DOTALL)

  17.         if not m:

  18.             main_d.callback(None)

  19.             return

  20.         url = m.group('url')

  21.         d = getPage(url)

  22.         d.addCallback(third_step, country, url)

  23.         d.addErrback(failure, country)

  24.    

  25.     def third_step(content, country, url):

  26.         m = re.search("", content)

  27.         if m:

  28.             title = m.group(1)

  29.             main_d.callback(dict(url = url, title = title))

  30.         else:

  31.             main_d.callback(dict(url=url, title="{not-specified}"))

  32.    

  33.     def failure(e, country):

  34.         print ".%s FAILED: %s" % (country, str(e))

  35.         main_d.callback(None)

  36.    

  37.     first_step()

  38.     return main_d

  39.    

  40. def printResult(result, country):

  41.     if result:

  42.         print ".%s result: %s (%s)" % (country, result['url'], result['title'])

  43.     else:

  44.         print ".%s result: nothing found" % country

  45.    

  46. def runme():

  47.     all = []

  48.     for country in ["com", "pl", "nonexistant"]:

  49.         d = lookup(country, "Twisted")

  50.         d.addCallback(printResult, country)

  51.         all.append(d)

  52.     defer.DeferredList(all).addCallback(lambda _: reactor.stop())

  53.    

  54. reactor.callLater(0, runme)

  55. reactor.run()

 

  1. inlineCallbacks:

  2. from twisted.internet import reactor, defer

  3. from twisted.web.client import getPage

  4. import re

  5.    

  6. @defer.inlineCallbacks

  7. def lookup(country, search_term):

  8.     try:

  9.         query = "%s/search?q=%s" % (country,search_term)

  10.         content = yield getPage(query)

  11.    

  12.         m = re.search('
    http://[^"]+)"',

  13.                       content, re.DOTALL)

  14.         if not m:

  15.             defer.returnValue(None)

  16.    

  17.         url = m.group('url')

  18.         content = yield getPage(url)

  19.    

  20.         m = re.search("", content)

  21.         if m:

  22.             defer.returnValue(dict(url=url, title=m.group(1)))

  23.         else:

  24.             defer.returnValue(dict(url=url, title="{not-specified}"))

  25.    

  26.     except Exception, e:

  27.         print ".%s FAILED: %s" % (country, str(e))

  28.    

  29. def printResult(result, country):

  30.     if result:

  31.         print ".%s result: %s (%s)" % (country, result['url'], result['title'])

  32.     else:

  33.         print ".%s result: nothing found" % country

  34.    

  35. def runme():

  36.     all = []

  37.     for country in ["com", "pl", "nonexistant"]:

  38.         d = lookup(country, "Twisted")

  39.         d.addCallback(printResult, country)

  40.         all.append(d)

  41.     defer.DeferredList(all).addCallback(lambda _: reactor.stop())

  42.    

  43. reactor.callLater(0, runme)

  44. reactor.run()
阅读(1829) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~