本文整理汇总了Python中twisted.internet.defer.DeferredList.addCallback方法的典型用法代码示例。如果您正苦于以下问题:Python DeferredList.addCallback方法的具体用法?Python DeferredList.addCallback怎么用?Python DeferredList.addCallback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.defer.DeferredList
的用法示例。
在下文中一共展示了DeferredList.addCallback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _copyDomainCallback3
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def _copyDomainCallback3(self, data, source_domain, destination_domain,
total_box_usage=0):
xml = ET.fromstring(data["response"])
box_usage = float(xml.find(".//%sBoxUsage" % SDB_NAMESPACE).text)
self.box_usage += box_usage
total_box_usage += box_usage
next_token_element = xml.find(".//%sNextToken" % SDB_NAMESPACE)
if next_token_element is not None:
next_token = next_token_element.text
else:
next_token = None
items = xml.findall(".//%sItem" % SDB_NAMESPACE)
results = {}
for item in items:
key = item.find("./%sName" % SDB_NAMESPACE).text
attributes = item.findall("%sAttribute" % SDB_NAMESPACE)
attribute_dict = {}
for attribute in attributes:
attr_name = attribute.find("./%sName" % SDB_NAMESPACE).text
attr_value = attribute.find("./%sValue" % SDB_NAMESPACE).text
if attr_name in attribute_dict:
attribute_dict[attr_name].append(attr_value)
else:
attribute_dict[attr_name] = [attr_value]
results[key] = attribute_dict
deferreds = []
for key in results:
d = self.putAttributes(destination_domain, key, results[key])
d.addErrback(self._copyPutAttributesErrback, destination_domain, key, results[key])
deferreds.append(d)
d = DeferredList(deferreds, consumeErrors=True)
d.addCallback(self._copyDomainCallback4, source_domain,
destination_domain, next_token=next_token, total_box_usage=total_box_usage)
return d
示例2: handle_nodes
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def handle_nodes(result, media_id, owner_username):
"""
I don't know what the hell this does. looks like nothing.
@return: Unknown
@rtype: Unknown
The above comment was added by Clint.
I left it here to illustrate something:
Clint's full of shit.
V
"""
if result[0] != 0:
raise errors.APIError(result[1])
nodes = result[1]
dl = []
for n in nodes:
d2 = self._make_media_path(media_id, n, owner_username)
d2.addCallback(store)
d2.addCallback(lambda _: self.clear_renders(media_id, owner_username, n))
dl.append(d2)
dList = DeferredList(dl)
dList.addCallback(lambda _: "success")
return dList
示例3: _checkBodies
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def _checkBodies(self, responses, callback, *params):
deferreds = [Deferred() for r in responses]
for i, (s, r) in enumerate(responses):
r.deliverBody(PrinterClient(deferreds[i]))
dl = DeferredList(deferreds)
dl.addCallback(callback, *params)
return dl
示例4: start_processes
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def start_processes(_):
logpool.log(
self.uuid,
"internal",
"Starting work on job %s, assignment of %s tasks."
% (self.assignment["job"]["title"], len(self.assignment["tasks"])),
)
self._before_start()
logger.debug("%r.start()", self.__class__.__name__)
try:
self.start()
self.start_called = True
logger.debug("Collecting started deferreds from spawned " "processes")
if not self.processes:
logger.warning("No processes have been started, firing deferreds " "immediately.")
self.started_deferred.callback(None)
self.stopped_deferred.callback(None)
else:
logger.debug("Making deferred list for %s started " "processes", len(self.processes))
processes_deferred = DeferredList([process.started for process in self.processes.values()])
processes_deferred.addCallback(lambda x: self.started_deferred.callback(x))
except Exception as e:
self.started_deferred.errback(e)
self.stopped_deferred.errback(e)
示例5: testInactiveDelay
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def testInactiveDelay(self):
'''all but one player is inactive. this player should win'''
self.createTourney(players_quota=5, players_min=5, seats_per_game=2, inactive_delay=1000)
tourney, clients = self.tourney, self.clients
tourney.changeState(TOURNAMENT_STATE_RUNNING)
serial = 1
client_online = None
table_online = None
dl = []
for client in clients.itervalues():
client.registerHandler(PACKET_POKER_TOURNEY_RANK, client.handleRank)
dl.append(client.finished)
dl = DeferredList(dl)
for game_id,game in tourney.id2game.items():
if serial in game.serial2player:
table_online = self.service.tables[game_id]
client_online = clients[serial]
client_online.registerHandler(client_online.filterPosition, client_online.handlePosition)
break
def checkForRank(res): self.assertEquals(tourney.winners[0], client_online.getSerial())
dl.addCallback(checkForRank)
table_online.joinPlayer(client_online)
table_online.update()
return dl
示例6: _start
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def _start(self):
deferreds = []
deferreds.append(self.getNetworkAddress())
if self.time_offset is None:
deferreds.append(self.getTimeOffset())
d = DeferredList(deferreds, consumeErrors=True)
d.addCallback(self._startCallback)
示例7: subscribe
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def subscribe(self):
def _logFailure(failure):
log.debug("reported {message}", message=failure.getErrorMessage())
return failure
def _logGrantedQoS(value):
log.debug("response {value!r}", value=value)
return True
def _logAll(*args):
log.debug("all subscriptions complete args={args!r}",args=args)
d1 = self.protocol.subscribe("foo/bar/baz1", 2 )
d1.addCallbacks(_logGrantedQoS, _logFailure)
d2 = self.protocol.subscribe("foo/bar/baz2", 2 )
d2.addCallbacks(_logGrantedQoS, _logFailure)
d3 = self.protocol.subscribe("foo/bar/baz3", 2 )
d3.addCallbacks(_logGrantedQoS, _logFailure)
dlist = DeferredList([d1,d2,d3], consumeErrors=True)
dlist.addCallback(_logAll)
return dlist
示例8: send_catch_log_deferred
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def send_catch_log_deferred(signal=Any, sender=Anonymous, *arguments, **named):
"""Like send_catch_log but supports returning deferreds on signal handlers.
Returns a deferred that gets fired once all signal handlers deferreds were
fired.
"""
def logerror(failure, recv):
if dont_log is None or not isinstance(failure.value, dont_log):
logger.error(
"Error caught on signal handler: %(receiver)s",
{"receiver": recv},
exc_info=failure_to_exc_info(failure),
extra={"spider": spider},
)
return failure
dont_log = named.pop("dont_log", None)
spider = named.get("spider", None)
dfds = []
for receiver in liveReceivers(getAllReceivers(sender, signal)):
d = maybeDeferred(robustApply, receiver, signal=signal, sender=sender, *arguments, **named)
d.addErrback(logerror, receiver)
d.addBoth(lambda result: (receiver, result))
dfds.append(d)
d = DeferredList(dfds)
d.addCallback(lambda out: [x[1] for x in out])
return d
示例9: trigger_convergence_groups
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def trigger_convergence_groups(authenticator, region, groups,
concurrency_limit, no_error_group):
"""
Trigger convergence on given groups
:param IAuthenticator authenticator: Otter authenticator
:param str region: Region where this is running
:param list groups: List of group dicts
:param int concurrency_limit: Concurrency limit
:param bool no_error_group: If true then do not converge ERROR groups
:return: Deferred fired with None
"""
sem = DeferredSemaphore(concurrency_limit)
d = DeferredList(
[sem.run(trigger_convergence, authenticator, region, group,
no_error_group)
for group in groups],
fireOnOneCallback=False,
fireOnOneErrback=False,
consumeErrors=True)
d.addCallback(
lambda results: [(g["tenantId"], g["groupId"], f.value)
for g, (s, f) in zip(groups, results) if not s])
return d
示例10: _storeData
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def _storeData(self,
data,
request_hash,
confirm_cache_write,
http_history=None):
if len(data["response"]) == 0:
return self._storeDataErrback(Failure(exc_value=Exception("Response data is of length 0")), data, request_hash)
#data["content-sha1"] = sha1(data["response"]).hexdigest()
if http_history is None:
http_history = {}
if "content-sha1" not in http_history:
http_history["content-sha1"] = data["content-sha1"]
if "content-changes" not in http_history:
http_history["content-changes"] = []
if data["content-sha1"] != http_history["content-sha1"]:
http_history["content-changes"].append(str(int(self.time_offset + time.time())))
http_history["content-changes"] = http_history["content-changes"][-10:]
headers = {}
http_history["content-changes"] = filter(lambda x:len(x) > 0, http_history["content-changes"])
headers["content-changes"] = ",".join(http_history["content-changes"])
headers["content-sha1"] = data["content-sha1"]
if "cache-control" in data["headers"]:
if isinstance(data["headers"]["cache-control"], (list, tuple)):
if "no-cache" in data["headers"]["cache-control"][0]:
return data
else:
if "no-cache" in data["headers"]["cache-control"]:
return data
if "expires" in data["headers"]:
if isinstance(data["headers"]["expires"], (list, tuple)):
headers["cache-expires"] = data["headers"]["expires"][0]
else:
headers["cache-expires"] = data["headers"]["expires"]
if "etag" in data["headers"]:
if isinstance(data["headers"]["etag"], (list, tuple)):
headers["cache-etag"] = data["headers"]["etag"][0]
else:
headers["cache-etag"] = data["headers"]["etag"]
if "last-modified" in data["headers"]:
if isinstance(data["headers"]["last-modified"], (list, tuple)):
headers["cache-last-modified"] = data["headers"]["last-modified"][0]
else:
headers["cache-last-modified"] = data["headers"]["last-modified"]
if "content-type" in data["headers"]:
if isinstance(data["headers"]["content-type"], (list, tuple)):
headers["content_type"] = data["headers"]["content-type"][0]
else:
headers["content_type"] = data["headers"]["content-type"]
headers_key = 'headers:%s' % request_hash
http_key = 'http:%s' % request_hash
logger.debug("Writing data for request %s to redis." % request_hash)
deferreds = []
deferreds.append(self.redis_client.set(headers_key, compress(json.dumps(headers), 1)))
deferreds.append(self.redis_client.set(http_key, compress(json.dumps(data["response"]), 1)))
d = DeferredList(deferreds, consumeErrors=True)
if confirm_cache_write:
d.addCallback(self._storeDataCallback, data)
d.addErrback(self._storeDataErrback, data, request_hash)
return d
return data
示例11: tearDown
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def tearDown(self):
deferreds = []
deferreds.append(self.spider.shutdown())
d = DeferredList(deferreds)
d.addCallback(self._tearDownCallback)
return d
示例12: connect
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def connect(self):
self.servers = []
serverDeferreds = []
for connection_info in self.connection_list:
try:
if type(connection_info) == dict:
def addServer(res):
self.servers.append(res)
return res
d = redis.Connection(**connection_info)
d.addCallback(addServer)
serverDeferreds.append(d)
else:
server = connection_info
self.servers.append(server)
except Exception as e:
raise Warning(str(e))
def checkQuorun(res):
self.quorum = (len(self.connection_list) // 2) + 1
if len(self.servers) < self.quorum:
raise CannotObtainLock(
"Failed to connect to the majority of redis servers")
return res
dl = DeferredList(serverDeferreds)
dl.addCallback(checkQuorun)
return dl
示例13: send_payment_request
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def send_payment_request(self, readTokens, writeTokens):
"""Called by a Circuit object when it wants to actually make a payment
@param readTokens: the number of read tokens to pay for at each hop in the circuit
@type readTokens: int
@param writeTokens: the number of read tokens to pay for at each hop in the circuit
@type writeTokens: int"""
assert (readTokens + writeTokens) / Globals.CELLS_PER_PAYMENT, "tried to pay for bad number of cells"
#make sure our setup is done:
if not self.setupDone:
#have we even started?
if not self.setupStarted:
self.send_setup_message()
self.queuedReadTokens += readTokens
self.queuedWriteTokens += writeTokens
return
#dont bother trying to send payments for circuits that are already closed
if self.circ.is_done():
return
#send the payments
deferreds = []
for paymentStream in self.paymentStreams.values():
deferreds.append(paymentStream.send_payment(readTokens, writeTokens))
paymentsDoneDeferred = DeferredList(deferreds)
paymentsDoneDeferred.addErrback(self.generic_error_handler)
addTokensDeferred = Deferred()
self.inflightReadTokens += readTokens
self.inflightWriteTokens += writeTokens
#timeout in case the payment fails. We will close the circuit in this case.
event = Scheduler.schedule_once(PaymentStream.PAR_TIMEOUT, self.all_receipts_received, None, addTokensDeferred, readTokens, writeTokens, None)
paymentsDoneDeferred.addCallback(self.all_receipts_received, addTokensDeferred, readTokens, writeTokens, event)
addTokensDeferred.addCallback(self._add_tokens_callback, readTokens, writeTokens)
addTokensDeferred.addErrback(self.generic_error_handler)
示例14: _reportstate_on_nodes
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def _reportstate_on_nodes(self, deployment):
"""
Connect to all nodes and run ``flocker-reportstate``.
:param Deployment deployment: The requested already parsed
configuration.
:return: ``Deferred`` that fires with a ``bytes`` in YAML format
describing the current configuration.
"""
command = [b"flocker-reportstate"]
results = []
for target in self._get_destinations(deployment):
d = deferToThread(target.node.get_output, command)
d.addCallback(safe_load)
d.addCallback(lambda val, key=target.hostname: (key, val))
results.append(d)
d = DeferredList(results, fireOnOneErrback=False, consumeErrors=True)
def got_results(node_states):
# Bail on errors:
for succeeded, value in node_states:
if not succeeded:
return value
return safe_dump(dict(pair for (_, pair) in node_states))
d.addCallback(got_results)
return d
示例15: client_connected
# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallback [as 别名]
def client_connected( protocol ):
proxy = Proxy( Test_Stub( protocol ), Math_Stub( protocol ))
request = EchoRequest()
request.text = "Hello world!"
echoed = proxy.Test.Echo( request )
echoed.addCallback( print_response )
request = PingRequest()
pinged = proxy.Test.Ping( request )
pinged.addCallback( print_response )
request = MathBinaryOperationRequest()
request.first = 2;
request.second = 2;
mathAddd = proxy.Math.Add( request )
mathAddd.addCallback( print_response )
mathMultiplyd = proxy.Math.Multiply( request )
mathMultiplyd.addCallback( print_response )
dl = DeferredList( [ echoed, pinged, mathAddd, mathMultiplyd ] )
dl.addCallback( client_finished )
return dl