当前位置: 首页>>代码示例>>Python>>正文


Python DeferredList.addCallbacks方法代码示例

本文整理汇总了Python中twisted.internet.defer.DeferredList.addCallbacks方法的典型用法代码示例。如果您正苦于以下问题:Python DeferredList.addCallbacks方法的具体用法?Python DeferredList.addCallbacks怎么用?Python DeferredList.addCallbacks使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.internet.defer.DeferredList的用法示例。


在下文中一共展示了DeferredList.addCallbacks方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: enqueue

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]
 def enqueue(self):
     now = int(time.time())
     # Compare the heap min timestamp with now().
     # If it's time for the item to be queued, pop it, update the
     # timestamp and add it back to the heap for the next go round.
     queue_items = []
     if self.amqp_queue_size < 100000:
         queue_items_a = queue_items.append
         LOGGER.debug("%s:%s" % (self.heap[0][0], now))
         while self.heap[0][0] < now and len(queue_items) < 1000:
             job = heappop(self.heap)
             uuid = UUID(bytes=job[1][0])
             if not uuid.hex in self.unscheduled_items:
                 queue_items_a(job[1][0])
                 new_job = (now + job[1][1], job[1])
                 heappush(self.heap, new_job)
             else:
                 self.unscheduled_items.remove(uuid.hex)
     else:
         LOGGER.critical('AMQP queue is at or beyond max limit (%d/100000)'
             % self.amqp_queue_size)
     # add items to amqp
     if queue_items:
         LOGGER.info('Found %d new uuids, adding them to the queue'
             % len(queue_items))
         msgs = [Content(uuid) for uuid in queue_items]
         deferreds = [self.chan.basic_publish(
             exchange=self.amqp_exchange, content=msg) for msg in msgs]
         d = DeferredList(deferreds, consumeErrors=True)
         d.addCallbacks(self._addToQueueComplete, self._addToQueueErr)
     else:
         self.enqueueCallLater = reactor.callLater(1, self.enqueue)
开发者ID:pombredanne,项目名称:awspider,代码行数:34,代码来源:scheduler.py

示例2: _newConnections

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]
    def _newConnections(self, conn_str):
        """ Make a pool of new connections. """
        # lock with the semaphore before calling this
        logging.debug("IndxConnectionPool _newConnections")
        return_d = Deferred()

        self.connections[conn_str] = DBConnectionPool(conn_str)

        try:
            d_list = []
            for i in range(MIN_CONNS):
                connection_d = self._newConnection(conn_str) 
                d_list.append(connection_d)

            dl = DeferredList(d_list)
            dl.addCallbacks(return_d.callback, return_d.errback)

        except Exception as e:
            logging.error("IndxConnectionPool error in _newConnections: {0}".format(e))
            return_d.errback(Failure(e))

        return return_d
开发者ID:sociam,项目名称:indx,代码行数:24,代码来源:connectionpool.py

示例3: _process_incoming_email

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]
    def _process_incoming_email(self, otherself, filepath, mask):
        """
        Callback that processes incoming email

        @param otherself: Watch object for the current callback from
        inotify
        @type otherself: twisted.internet.inotify._Watch
        @param filepath: Path of the file that changed
        @type filepath: twisted.python.filepath.FilePath
        @param mask: identifier for the type of change that triggered
        this callback
        @type mask: int
        """
        if os.path.split(filepath.dirname())[-1]  == "new":
            log.msg("Processing new mail at %s" % (filepath.path,))
            with filepath.open("r") as f:
                mail_data = f.read()
                mail = message_from_string(mail_data)
                owner = mail["To"]
                if owner is None:  # default to Delivered-To
                    owner = mail["Delivered-To"]
                if owner is None:
                    log.err("Malformed mail, neither To: nor "
                            "Delivered-To: field")
                log.msg("Mail owner: %s" % (owner,))

                log.msg("%s received a new mail" % (owner,))
                dpubk = self._users_cdb.getPubKey(owner)
                duuid = self._users_cdb.queryByAddress(owner)
                d = DeferredList([dpubk, duuid])
                d.addCallbacks(self._gather_uuid_pubkey, log.err)
                d.addCallbacks(self._encrypt_message, log.err,
                               (owner, mail_data))
                d.addCallbacks(self._export_message, log.err)
                d.addCallbacks(self._conditional_remove, log.err,
                               (filepath,))
                d.addErrback(log.err)
开发者ID:kalikaneko,项目名称:leap_mx,代码行数:39,代码来源:mail_receiver.py

示例4: test_minimal

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]

#.........这里部分代码省略.........
            one_done = Deferred()
            config.all_done.append(one_done)

            def make_make(Klass, done):
               def make(config):
                  c = Klass(config, done)
                  config.components.append(c)
                  return c
               return make

            ## .. and set the session class on the factory
            ##
            session_factory.session = make_make(C, one_done)

            if self.transport == "websocket":

               serializers = [JsonSerializer()]

               ## create a WAMP-over-WebSocket transport client factory
               ##
               transport_factory = WampWebSocketClientFactory(session_factory, serializers = serializers, url = self.url, debug_wamp = self.debug)

               if True:
                  def maker(Klass):
                     class TestClientProtocol(WampWebSocketClientProtocol):
                        def onOpen(self):
                           self.txcnt = 0
                           self.rxcnt = 0
                           WampWebSocketClientProtocol.onOpen(self)

                        def sendMessage(self, payload, isBinary):
                           self.txcnt += 1
                           print("> : {0:>3} : {1:<20} : {3}".format(self.txcnt, Klass.__name__, payload))
                           WampWebSocketClientProtocol.sendMessage(self, payload, isBinary)

                        def onMessage(self, payload, isBinary):
                           self.rxcnt += 1
                           print("< : {0:>3} : {1:<20} : {2}".format(self.rxcnt, Klass.__name__, payload))
                           WampWebSocketClientProtocol.onMessage(self, payload, isBinary)
                     return TestClientProtocol

                  transport_factory.protocol = maker(C)
               else:
                  transport_factory.protocol = WampWebSocketClientProtocol

               transport_factory.setProtocolOptions(failByDrop = False, openHandshakeTimeout = 0, closeHandshakeTimeout = 0)

            elif self.transport in ['rawsocket-json', 'rawsocket-msgpack']:

               ## create a WAMP-over-RawSocket transport client factory
               ##
               if self.transport == 'rawsocket-msgpack':
                  serializer = MsgPackSerializer()
               elif self.transport == 'rawsocket-json':
                  serializer = JsonSerializer()
               else:
                  raise Exception("should not arrive here")

               transport_factory = WampRawSocketClientFactory(session_factory, serializer, debug = self.debug)


            ## start the client from an endpoint
            ##
            cl = clientFromString(reactor, self.client)
            clients_d.append(cl.connect(transport_factory))

            clients.append(cl)

         config.connected_clients = None

         def client_connected(res):
            config.connected_clients = [proto for success, proto in res if success]

         DeferredList(clients_d).addCallback(client_connected)


         d = DeferredList(config.all_done, consumeErrors = True)
         #d = config.components[1]._done

         def done(_):
            log.flush()
            log.close()
            if config.port:
               config.port.stopListening()
            if config.connected_clients:
               for proto in config.connected_clients:
                  proto.transport.abortConnection()
            print("Log length: {0}".format(len(config.dlog)))
            print(config.dlog)
            #from twisted.internet import reactor
            #reactor.callLater(1, reactor.stop)

         def error(err):
            print(err)

         d.addCallbacks(done, error)

   #      d2 = Deferred()

         return d
开发者ID:BigSab,项目名称:AutobahnPython,代码行数:104,代码来源:_test_component.py

示例5: range

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]
      df = []
      for i in range(options.workers):

         args = [options.wsuri,
                 str(options.threads),
                 str(options.conns),
                 str(options.lowmark),
                 str(options.highmark),
                 options.resultfile % i]

         ## run wsperf executable
         d = getProcessOutput(options.wsperf, args, os.environ)

         ## accumulate any output
         df.append(d)

      d = DeferredList(df, consumeErrors = True)

      def onok(res):
         if not options.skipanalyze:
            analyze.printResults(resultfiles)
         reactor.stop()

      def onerr(err):
         print err
         reactor.stop()

      d.addCallbacks(onok, onerr)

      reactor.run()
开发者ID:crossbario,项目名称:wsperf,代码行数:32,代码来源:wsperf.py

示例6: make_entry

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]
        """
        entry = make_entry(logid, ts, app, level, msg, src_names, dst_names)
        l = []
        for f in self.filters:
            try:
                r = f(entry)
                if isinstance(r, Deferred):
                    l.append(r)
            except Exception, e:
                self.report([(False, e)])

        dl = DeferredList(l, fireOnOneCallback=False,
                          fireOnOneErrback=False,
                          consumeErrors=True)
        dl.addCallback(self.report) # DeferredList never calls an errback
        dl.addCallbacks(defer.callback, defer.errback)

        #process_ = lambda logid, ts, app, level, msg, src_names, dst_names:\
        #    process(logid, ts, app, level, msg, src_names, dst_names, d)
        #
        #self.uel.get_log_entry(id, process_)
        #return d

    def report(self, results):
        for result in results:
            if not result[0]:
                # Unfortunatively, we can't print out the exact
                # Failure (and included stack trace) due to a bug
                # in Twisted mail library.  It'll result in an
                # exception within the library:
                #
开发者ID:bigswitch,项目名称:snac,代码行数:33,代码来源:notifier.py

示例7: exchange

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]
 def exchange((xi, rhoi1, rhoi2)):
     # Send share to all receivers.
     ds = runtime.broadcast(receivers, receivers, str((str(xi), str(rhoi1), str(rhoi2))))
     dls = DeferredList(ds)
     dls.addCallbacks(check, runtime.error_handler)
     return dls
开发者ID:MaxFangX,项目名称:viff,代码行数:8,代码来源:test_hash_broadcast.py

示例8: __init__

# 需要导入模块: from twisted.internet.defer import DeferredList [as 别名]
# 或者: from twisted.internet.defer.DeferredList import addCallbacks [as 别名]
class asyncScanner:
    def __init__(self, ips, ports, max_threads):
        self.ips = ips
        self.ports = ports
        self.max_threads = max_threads
        self.scan_complete = False
        
        # this is a semaphore to limit the number of concurrent requests.
        #self.semaphore = DeferredSemaphore(self.max_threads)
        
        self.targets_list = Targets(ips, ports)
        self.ok = []
        self.error = []
        self.count = 0
        for ip in self.ips:
            # create target
            for port in ports:
                self.count += 1
        self.scan()
        
    def CheckPort(self, host, port, max_threads, timeout=10):
        d=Deferred()
        factory = ScanClientFactory(d, host, port)
        reactor.connectTCP(host, port, factory, timeout)
        return d
                 
    def scan(self):
        self.scan_list = list()
        for ip in self.ips:
            for port in self.ports: 
                # make a deferred job to check the host
                d = self.CheckPort(ip, port, self.max_threads)
                
                d.addCallbacks(self.scan_success, self.scan_failure)
                
                d.addBoth(self.scan_completed)
                
                self.scan_list.append(d)
                 
        self.dl = DeferredList(self.scan_list)
    
        return self.dl.addCallbacks(self.jobSuccess, self.jobError)
    
    def get_results(self):
        if self.scan_complete:
            return self.ok
        else:
            return None
    
    ### Count number of success    
    def scan_success(self, result):
        self.ok.append(result)
        
    ### Count number of errors    
    def scan_failure(self, reason):
        self.error.append(reason)
        
    # When a scan completes, check if it's the last one... to stop the reactor    
    def scan_completed(self, data):
         if len(self.ok) + len(self.error) == self.count:
             reactor.stop()
 
    def jobSuccess(self, x):
        self.scan_complete = True
        return True

         
    def jobError(self, reason):
        return False
        #print 'Scan Error'
开发者ID:svigneux,项目名称:scavenger,代码行数:72,代码来源:IPScanner.py


注:本文中的twisted.internet.defer.DeferredList.addCallbacks方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。