小编Jod*_*y S的帖子

使python程序等到Twisted deferred返回一个值

我有一个程序从其他页面获取信息并使用BeautifulSoup和Twisted的getPage解析它们.稍后在程序中我打印延迟进程创建的信息.目前我的程序试图在不同的返回信息之前打印它.我怎么能让它等待?

def twisAmaz(contents): #This parses the page (amazon api xml file)
    stonesoup = BeautifulStoneSoup(contents)
    if stonesoup.find("mediumimage") == None:
       imageurl.append("/images/notfound.png")
    else:
      imageurl.append(stonesoup.find("mediumimage").url.contents[0])

    usedPdata = stonesoup.find("lowestusedprice")
    newPdata = stonesoup.find("lowestnewprice")
    titledata = stonesoup.find("title")
    reviewdata = stonesoup.find("editorialreview")

    if stonesoup.find("asin") != None:
        asin.append(stonesoup.find("asin").contents[0])
    else:
        asin.append("None")
    reactor.stop()


deferred = dict()
for tmpISBN in isbn:  #Go through ISBN numbers and get Amazon API information for each
    deferred[(tmpISBN)] = getPage(fetchInfo(tmpISBN))
    deferred[(tmpISBN)].addCallback(twisAmaz)
    reactor.run()

.....print info on each ISBN
Run Code Online (Sandbox Code Playgroud)

python twisted deferred

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

deferred ×1

python ×1

twisted ×1