让 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
I have a program that fetches info from other pages and parses them using BeautifulSoup and Twisted's getPage. Later on in the program I print info that the deferred process creates. Currently my program tries to print it before the differed returns the info. How can I make it wait?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来你正在尝试制造/运行多个反应堆。所有东西都连接到同一个反应器。以下是如何使用
DeferredList
等待所有回调完成。另请注意,
twisAmaz
返回一个值。该值通过callbacks
DeferredList
传递,并作为value
输出。由于DeferredList
保留放入其中的内容的顺序,因此您可以将结果索引与 ISBN 索引交叉引用。What it seems like is you're trying to make/run multiple reactors. Everything gets attached to the same reactor. Here's how to use a
DeferredList
to wait for all of your callbacks to finish.Also note that
twisAmaz
returns a value. That value is passed through thecallbacks
DeferredList
and comes out asvalue
. Since aDeferredList
keeps the order of the things that are put into it, you can cross-reference the index of the results with the index of your ISBNs.另一种很酷的方法是使用@defer.inlineCallbacks。它允许您像常规顺序函数一样编写异步代码: http ://twistedmatrix.com/documents/8.1.0/api/twisted.internet.defer.html#inlineCallbacks
Another cool way to do this is with @defer.inlineCallbacks. It lets you write asynchronous code like a regular sequential function: http://twistedmatrix.com/documents/8.1.0/api/twisted.internet.defer.html#inlineCallbacks
首先,您不应该在延迟方法中放置reactor.stop(),因为它会杀死所有内容。
现在,在 Twisted 中,“等待”是不允许的。要打印回调的结果,只需在第一个回调之后添加另一个回调即可。
First, you shouldn't put a reactor.stop() in your deferred method, as it kills everything.
Now, in Twisted, "Waiting" is not allowed. To print results of you callback, just add another callback after the first one.