本文整理汇总了Python中twisted.internet.task.cooperate函数的典型用法代码示例。如果您正苦于以下问题:Python cooperate函数的具体用法?Python cooperate怎么用?Python cooperate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cooperate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: disconnectAll
def disconnectAll(self):
reactor.runUntilCurrent()
def _disconnectAll():
for subscriber in self.connections_ready:
yield subscriber.transport.loseConnection()
task.cooperate(_disconnectAll())
示例2: main
def main():
print "Starting up..."
state = SharedState()
bot = BotFactory(config, state)
reactor.connectSSL(config.get('server'), config.get('port'), bot, ssl.ClientContextFactory())
cooperate(SlackBot(state, config).listen_to_slack())
reactor.run()
示例3: update_chunks
def update_chunks(self):
x, y, z = self.location.pos.to_block()
x, chaff, z, chaff = split_coords(x, z)
new = set((i + x, j + z) for i, j in circle)
old = set(self.chunks.iterkeys())
added = new - old
discarded = old - new
# Perhaps some explanation is in order.
# The cooperate() function iterates over the iterable it is fed,
# without tying up the reactor, by yielding after each iteration. The
# inner part of the generator expression generates all of the chunks
# around the currently needed chunk, and it sorts them by distance to
# the current chunk. The end result is that we load chunks one-by-one,
# nearest to furthest, without stalling other clients.
if self.chunk_tasks:
for task in self.chunk_tasks:
try:
task.stop()
except (TaskDone, TaskFailed):
pass
self.chunk_tasks = [
cooperate(
self.enable_chunk(i, j) for i, j in
sorted(added, key=lambda t: (t[0] - x)**2 + (t[1] - z)**2)
),
cooperate(self.disable_chunk(i, j) for i, j in discarded)
]
示例4: setup
def setup(self):
sb = self.blackboard.bot_standing_on_block(self.blackboard.bot_object)
while sb is None:
yield utils.reactor_break()
sb = self.blackboard.bot_standing_on_block(self.blackboard.bot_object)
else:
if self.travel_multiple_goals is not None:
d = cooperate(AStarMultiCoords(dimension=self.blackboard.dimension,
start_coords=sb.coords,
goal_coords=self.travel_coords,
multiple_goals=self.travel_multiple_goals)).whenDone()
elif self.travel_coords is not None:
d = cooperate(AStarCoords(dimension=self.blackboard.dimension,
start_coords=sb.coords,
goal_coords=self.travel_coords)).whenDone()
else:
d = cooperate(AStarBBCol(dimension=self.blackboard.dimension,
start_coords=sb.coords,
bb=self.travel_bb)).whenDone()
d.addErrback(logbot.exit_on_error)
astar = yield d
if astar.path is not None:
current_start = self.blackboard.bot_standing_on_block(self.blackboard.bot_object)
if sb == current_start:
self.path = astar.path
if self.shorten_path_by > 0:
self.path = self.path[self.shorten_path_by:]
self.start_coords = current_start.coords
示例5: pushInfoToSubscribers
def pushInfoToSubscribers(self):
# Generate the json doc
vars = {}
for subscriber in self.connections_ready:
subscriber_vars = subscriber.vars.copy()
subscriber_vars['port'] = subscriber.id + 12000
subscriber_vars['host'] = subscriber.transport.getPeer().host
vars[subscriber.id] = subscriber_vars
json_vars = json.dumps(vars)
del vars
msg("Pushing a %d bytes long json doc." % len(json_vars))
# Send the json doc to the subscribers
task.cooperate(self._sendLineToAllGenerator(json_vars))
示例6: test_onDisconnect_waitForOutstandingMessagesToFinish
def test_onDisconnect_waitForOutstandingMessagesToFinish(self):
config = self.getConfig(StompSpec.VERSION_1_0)
client = async.Stomp(config)
client.add(ReceiptListener(1.0))
# connect
yield client.connect(host=VIRTUALHOST)
yield task.cooperate(
iter([client.send(self.queue, self.frame, receipt="message-%d" % j) for j in range(self.numMsgs)])
).whenDone()
client.subscribe(
self.queue,
{StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL},
listener=SubscriptionListener(self._frameHandler),
)
# wait for disconnect
yield client.disconnected
# reconnect and subscribe again to make sure that all messages in the queue were ack'ed
yield client.connect(host=VIRTUALHOST)
self.timeExpired = False
self.timeoutDelayedCall = reactor.callLater(1, self._timesUp, client) # @UndefinedVariable
client.subscribe(
self.queue,
{StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL},
listener=SubscriptionListener(self._eatOneFrameAndDisconnect),
)
# wait for disconnect
yield client.disconnected
# time should have expired if there were no messages left in the queue
self.assertTrue(self.timeExpired)
示例7: startStreaming
def startStreaming(self):
"""
This should be called by the consumer when the producer is registered.
Start streaming data to the consumer.
"""
self._coopTask = cooperate(self._pull())
示例8: connectionMade
def connectionMade(self):
print("Connected protocol")
self.queue = Queue.Queue()
#self.transport.registerProducer(QueueProducer(self.transport), False)
self.sendLine("3,0,0")
gen = DoodleQueueWorkload(self.transport.reactor, self)
self.task = cooperate(gen)
示例9: searchIndices
def searchIndices(indices, query, period, lastId=None, reverse=False, fields=None, limit=100):
"""
Search the specified indices for events matching the specified query.
:param indices: A list of indices to search.
:type indices: A list of objects implementing :class:`terane.bier.index.IIndex`
:param query: The programmatic query to use for searching the indices.
:type query: An object implementing :class:`terane.bier.searching.IQuery`
:param period: The period within which the search is constrained.
:type period: :class:`terane.bier.searching.Period`
:param lastId: The real key to start iterating from.
:type lastId: :class:`terane.bier.evid.EVID`
:param reverse: If True, then reverse the order of events.
:type reverse: bool
:param fields: If not None, then only return the specified fields of each event.
:type fields: list or None
:param limit: Only returned the specified number of events.
:type limit: int
:returns: A CooperativeTask which contains a Deferred and manages the search task.
:rtype: :class:`twisted.internet.task.CooperativeTask`
"""
start = time.time()
# determine the evids to use as start and end keys
if reverse == False:
startId, endId = period.getRange()
else:
endId, startId = period.getRange()
if lastId != None:
if not lastId in period:
raise SearcherError("lastId %s is not within period" % lastId)
startId = lastId
# search each index separately, then merge the results
try:
searchers = []
postingLists = []
for index in indices:
if not IIndex.providedBy(index):
raise TypeError("index does not implement IIndex")
# we create a copy of the original query, which can possibly be optimized
# with index-specific knowledge.
_query = copy.deepcopy(query)
try:
_query = _query.optimizeMatcher(index)
except NotImplementedError, e:
raise SearcherError(str(e))
logger.debug("optimized query for index '%s': %s" % (index.name, str(_query)))
# if the query optimized out entirely, then skip to the next index
if _query == None:
continue
# get the posting list to iterate through
searcher = index.newSearcher()
if not ISearcher.providedBy(searcher):
raise TypeError("searcher does not implement ISearcher")
postingList = _query.iterMatches(searcher, startId, endId)
if not IPostingList.providedBy(postingList):
raise TypeError("posting list does not implement IPostingList")
searchers.append(searcher)
postingLists.append(postingList)
# return a cooperative task
return cooperate(ResultSet(searchers, postingLists, start, reverse, fields, limit))
示例10: startProducing
def startProducing(self, consumer):
self._consumer = consumer
self._iterable = SmapEncoder().iterencode(self._value)
self._task = cooperate(self._produce())
d = self._task.whenDone()
d.addBoth(self._unregister)
return d
示例11: _disconnect
def _disconnect(self, receipt, failure, timeout):
if failure:
self._disconnectReason = failure
self.log.info('Disconnecting ...%s' % ('' if (not failure) else ('[reason=%s]' % failure)))
protocol = self._protocol
try:
# notify that we are ready to disconnect after outstanding messages are ack'ed
if self._messages:
self.log.info('Waiting for outstanding message handlers to finish ... [timeout=%s]' % timeout)
try:
yield task.cooperate(iter([wait(handler, timeout, StompCancelledError('Going down to disconnect now')) for handler in self._messages.values()])).whenDone()
except StompCancelledError as e:
self._disconnectReason = StompCancelledError('Handlers did not finish in time.')
else:
self.log.info('All handlers complete. Resuming disconnect ...')
if self.session.state == self.session.CONNECTED:
frame = self.session.disconnect(receipt)
try:
self.sendFrame(frame)
except Exception as e:
self._disconnectReason = StompConnectionError('Could not send %s. [%s]' % (frame.info(), e))
try:
yield self._waitForReceipt(receipt)
except StompCancelledError:
self._disconnectReason = StompCancelledError('Receipt for disconnect command did not arrive on time.')
protocol.loseConnection()
except Exception as e:
self._disconnectReason = e
示例12: send_initial_chunk_and_location
def send_initial_chunk_and_location(self):
bigx, smallx, bigz, smallz = split_coords(self.location.x,
self.location.z)
# Spawn the 25 chunks in a square around the spawn, *before* spawning
# the player. Otherwise, there's a funky Beta 1.2 bug which causes the
# player to not be able to move.
d = cooperate(
self.enable_chunk(i, j)
for i, j in product(
xrange(bigx - 3, bigx + 3),
xrange(bigz - 3, bigz + 3)
)
).whenDone()
# Don't dare send more chunks beyond the initial one until we've
# spawned.
d.addCallback(lambda none: self.update_location())
d.addCallback(lambda none: self.position_changed())
# Send the MOTD.
if self.motd:
packet = make_packet("chat",
message=self.motd.replace("<tagline>", get_motd()))
d.addCallback(lambda none: self.transport.write(packet))
# Finally, start the secondary chunk loop.
d.addCallback(lambda none: self.update_chunks())
示例13: startProducing
def startProducing(self, consumer):
""" Must NOT call registerProducer on the consumer """
self._consumer = consumer
self._iterable = json.JSONEncoder().iterencode(self.body)
self._task = cooperate(self._produce())
d = self._task.whenDone()
return d
示例14: _driveWorker
def _driveWorker(self, worker, result, testCases, cooperate):
"""
Drive a L{LocalWorkerAMP} instance, iterating the tests and calling
C{run} for every one of them.
@param worker: The L{LocalWorkerAMP} to drive.
@param result: The global L{DistReporter} instance.
@param testCases: The global list of tests to iterate.
@param cooperate: The cooperate function to use, to be customized in
tests.
@type cooperate: C{function}
@return: A C{Deferred} firing when all the tests are finished.
"""
def resultErrback(error, case):
result.original.addFailure(case, error)
return error
def task(case):
d = worker.run(case, result)
d.addErrback(resultErrback, case)
return d
return cooperate(task(case) for case in testCases).whenDone()
示例15: main
def main(reactor, *argv):
parameters = ConnectionParameters.fromCommandLine(reactor, argv)
endpoint = parameters.endpointForCommand(b"/bin/cat")
done = []
factory = Factory()
factory.protocol = Protocol
d = endpoint.connect(factory)
def gotConnection(proto):
conn = proto.transport.conn
for i in range(50):
factory = Factory()
factory.protocol = PrinterProtocol
factory.done = Deferred()
done.append(factory.done)
e = SSHCommandClientEndpoint.existingConnection(
conn, b"/bin/echo %d" % (i,))
yield e.connect(factory)
d.addCallback(gotConnection)
d.addCallback(lambda work: cooperate(work).whenDone())
d.addCallback(lambda ignored: gatherResults(done))
return d