我使用txmongo lib作爲mongoDB的驅動程序。 在其有限的文檔中,txmongo中的find函數將返回一個延遲實例,但是如何獲得實際結果(如{「IP」:11.12.59.119})?我嘗試了yield,str()和repr()但不起作用。如何從返回的延遲實例中獲取值
def checkResource(self, resource):
""" use the message to inquire database
then set the result to a ip variable
"""
d = self.units.find({'$and': [{'baseIP':resource},{'status':'free'}]},limit=1,fields={'_id':False,'baseIP':True})
#Here above, how can I retrieve the result in this deferred instance??
d.addCallback(self.handleReturnedValue)
d.addErrback(log.err)
return d
def handleReturnedValue(self, returned):
for ip in returned:
if ip is not None:
d = self.updateData(ip,'busy')
return d
else:
return "NA"
你必須使用一個回調,以便值在handleReturnedValue HTTPS訪問:// twistedmatrix。 com/documents/16.0.0/core/howto/defer.html –