【发布时间】:2020-08-10 23:44:43
【问题描述】:
我是使用 Twisted 库的新手,我想制作一个异步操作列表。以下面的伪代码为例:
@defer.inlineCallbacks
def getDataAsync(host):
data = yield AsyncHttpAPI(host) # some asyc api which returns deferred
return data
@defer.inlineCallbacks
def funcPrintData():
hosts = []; # some list of hosts, say 1000 in number
for host in hosts:
data = yield getDataAsync(host)
# why doesn't the following line get printed as soon as first result is available
# it waits for all getDataAsync to be queued before calling the callback and so print data
print(data)
如果问题不清楚,请发表评论。有没有更好的方法来做到这一点?我应该改用 DeferredList 吗?
【问题讨论】:
标签: python twisted twisted.internet