Python twisted.internet.defer 模块,DeferredList() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用twisted.internet.defer.DeferredList()

项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def logOn(self, chatui):
        """
        @returns: this breaks with L{interfaces.IAccount}
        @returntype: DeferredList of L{interfaces.IClient}s
        """
        # Overriding basesupport's implementation on account of the
        # fact that _startLogOn tends to return a deferredList rather
        # than a simple Deferred, and we need to do registerAccountClient.
        if (not self._isConnecting) and (not self._isOnline):
            self._isConnecting = 1
            d = self._startLogOn(chatui)
            d.addErrback(self._loginFailed)
            def registerMany(results):
                for success, result in results:
                    if success:
                        chatui.registerAccountClient(result)
                        self._cb_logOn(result)
                    else:
                        log.err(result)
            d.addCallback(registerMany)
            return d
        else:
            raise error.ConnectionError("Connection in progress")
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def _cbGetFileSize(self, attrs, rf, lf):
        if not stat.S_ISREG(attrs['permissions']):
            rf.close()
            lf.close()
            return "Can't get non-regular file: %s" % rf.name
        rf.size = attrs['size']
        bufferSize = self.client.transport.conn.options['buffersize']
        numRequests = self.client.transport.conn.options['requests']
        rf.total = 0.0
        dList = []
        chunks = []
        startTime = time.time()
        for i in range(numRequests):            
            d = self._cbGetRead('', rf, lf, chunks, 0, bufferSize, startTime)
            dList.append(d)
        dl = defer.DeferredList(dList, fireOnOneErrback=1)
        dl.addCallback(self._cbGetDone, rf, lf)
        return dl
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def testResults(self):
        inputOutput = [
            ("add", (2, 3), 5),
            ("defer", ("a",), "a"),
            ("dict", ({"a": 1}, "a"), 1),
            ("triple", ("a", 1), ["a", 1, None])]

        dl = []
        for meth, args, outp in inputOutput:
            d = self.proxy().callRemote(meth, *args)
            d.addCallback(self.assertEquals, outp)
            dl.append(d)

        # SOAPpy kinda blows.
        d = self.proxy().callRemote('complex')
        d.addCallback(lambda result: result._asdict())
        d.addCallback(self.assertEquals, {"a": ["b", "c", 12, []], "D": "foo"})
        dl.append(d)

        # We now return to our regularly scheduled program, already in progress.
        return defer.DeferredList(dl, fireOnOneErrback=True)
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def testCooperation(self):
        L = []
        def myiter(things):
            for th in things:
                L.append(th)
                yield None

        groupsOfThings = ['abc', (1, 2, 3), 'def', (4, 5, 6)]

        c = task.Cooperator()
        tasks = []
        for stuff in groupsOfThings:
            tasks.append(c.coiterate(myiter(stuff)))

        return defer.DeferredList(tasks).addCallback(
            lambda ign: self.assertEquals(tuple(L), sum(zip(*groupsOfThings), ())))
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def testNotLoggedInReply(self):
        """When not logged in, all commands other than USER and PASS should
        get NOT_LOGGED_IN errors.
        """
        commandList = ['CDUP', 'CWD', 'LIST', 'MODE', 'PASV',
                       'PWD', 'RETR', 'STRU', 'SYST', 'TYPE']

        # Issue commands, check responses
        def checkResponse(exception):
            failureResponseLines = exception.args[0]
            self.failUnless(failureResponseLines[-1].startswith("530"),
                            "Response didn't start with 530: %r"
                            % (failureResponseLines[-1],))
        deferreds = []
        for command in commandList:
            deferred = self.client.queueStringCommand(command)
            self.assertFailure(deferred, ftp.CommandFailed)
            deferred.addCallback(checkResponse)
            deferreds.append(deferred)
        return defer.DeferredList(deferreds, fireOnOneErrback=True)
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def setUp(self):
        from twisted.internet import reactor
        self.serverFactory = protocol.ServerFactory()
        self.serverFactory.protocol = self.serverProto
        self.clientFactory = protocol.ClientFactory()
        self.clientFactory.protocol = self.clientProto
        self.clientFactory.onMade = defer.Deferred()
        self.serverFactory.onMade = defer.Deferred()
        self.serverPort = reactor.listenTCP(0, self.serverFactory)
        self.clientConn = reactor.connectTCP(
            '127.0.0.1', self.serverPort.getHost().port,
            self.clientFactory)
        def getProtos(rlst):
            self.cli = self.clientFactory.theProto
            self.svr = self.serverFactory.theProto
        dl = defer.DeferredList([self.clientFactory.onMade,
                                 self.serverFactory.onMade])
        return dl.addCallback(getProtos)
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def testDeferredListWithAlreadyFiredDeferreds(self):
        # Create some deferreds, and err one, call the other
        d1 = defer.Deferred()
        d2 = defer.Deferred()
        d1.errback(GenericError('Bang'))
        d2.callback(2)

        # *Then* build the DeferredList
        dl = defer.DeferredList([d1, d2])

        result = []
        dl.addCallback(result.append)

        self.failUnlessEqual(1, len(result))

        d1.addErrback(lambda e: None)  # Swallow error
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def test_concurrentRetrieves(self):
        """
        Issue three retrieve calls immediately without waiting for any to
        succeed and make sure they all do succeed eventually.
        """
        p, t = setUp()
        messages = [
            p.retrieve(i).addCallback(
                self.assertEquals,
                ["First line of %d." % (i + 1,),
                 "Second line of %d." % (i + 1,)])
            for i
            in range(3)]

        for i in range(1, 4):
            self.assertEquals(t.value(), "RETR %d\r\n" % (i,))
            t.clear()
            p.dataReceived("+OK 2 lines on the way\r\n")
            p.dataReceived("First line of %d.\r\n" % (i,))
            p.dataReceived("Second line of %d.\r\n" % (i,))
            self.assertEquals(t.value(), "")
            p.dataReceived(".\r\n")

        return defer.DeferredList(messages, fireOnOneErrback=True)
项目:onkyo_serial    作者:blaedd    | 项目源码 | 文件源码
def stopService(self):
        def stop_cb():
            if self._connector is not None:
                self._connector.disconnect()
                del self._connector

        service.Service.stopService(self)
        l = []
        for svc in reversed(list(self)):
            l.append(defer.maybeDeferred(svc.stopService))
        if l:
            l = defer.DeferredList(l)
            l.addCallback(stop_cb)
        else:
            stop_cb()
        return l
项目:landscape-client    作者:CanonicalLtd    | 项目源码 | 文件源码
def fetch_many_async(urls, callback=None, errback=None, **kwargs):
    """
    Retrieve a list of URLs asynchronously.

    @param callback: Optionally, a function that will be fired one time for
        each successful URL, and will be passed its content and the URL itself.
    @param errback: Optionally, a function that will be fired one time for each
        failing URL, and will be passed the failure and the URL itself.
    @return: A C{DeferredList} whose callback chain will be fired as soon as
        all downloads have terminated. If an error occurs, the errback chain
        of the C{DeferredList} will be fired immediatly.
    """
    results = []
    for url in urls:
        result = fetch_async(url, **kwargs)
        if callback:
            result.addCallback(callback, url)
        if errback:
            result.addErrback(errback, url)
        results.append(result)
    return DeferredList(results, fireOnOneErrback=True, consumeErrors=True)
项目:pysnmp    作者:etingof    | 项目源码 | 文件源码
def getSystem(reactor, hostname):
    snmpEngine = SnmpEngine()

    def getScalar(objectType):
        d = getCmd(snmpEngine,
                   CommunityData('public', mpModel=0),
                   UdpTransportTarget((hostname, 161)),
                   ContextData(),
                   objectType)
        d.addCallback(success, hostname).addErrback(failure, hostname)
        return d

    return DeferredList(
        [getScalar(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))),
         getScalar(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))]
    )
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def logOn(self, chatui):
        """
        @returns: this breaks with L{interfaces.IAccount}
        @returntype: DeferredList of L{interfaces.IClient}s
        """
        # Overriding basesupport's implementation on account of the
        # fact that _startLogOn tends to return a deferredList rather
        # than a simple Deferred, and we need to do registerAccountClient.
        if (not self._isConnecting) and (not self._isOnline):
            self._isConnecting = 1
            d = self._startLogOn(chatui)
            d.addErrback(self._loginFailed)
            def registerMany(results):
                for success, result in results:
                    if success:
                        chatui.registerAccountClient(result)
                        self._cb_logOn(result)
                    else:
                        log.err(result)
            d.addCallback(registerMany)
            return d
        else:
            raise error.ConnectionError("Connection in progress")
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def _cbGetFileSize(self, attrs, rf, lf):
        if not stat.S_ISREG(attrs['permissions']):
            rf.close()
            lf.close()
            return "Can't get non-regular file: %s" % rf.name
        rf.size = attrs['size']
        bufferSize = self.client.transport.conn.options['buffersize']
        numRequests = self.client.transport.conn.options['requests']
        rf.total = 0.0
        dList = []
        chunks = []
        startTime = time.time()
        for i in range(numRequests):            
            d = self._cbGetRead('', rf, lf, chunks, 0, bufferSize, startTime)
            dList.append(d)
        dl = defer.DeferredList(dList, fireOnOneErrback=1)
        dl.addCallback(self._cbGetDone, rf, lf)
        return dl
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def testResults(self):
        inputOutput = [
            ("add", (2, 3), 5),
            ("defer", ("a",), "a"),
            ("dict", ({"a": 1}, "a"), 1),
            ("triple", ("a", 1), ["a", 1, None])]

        dl = []
        for meth, args, outp in inputOutput:
            d = self.proxy().callRemote(meth, *args)
            d.addCallback(self.assertEquals, outp)
            dl.append(d)

        # SOAPpy kinda blows.
        d = self.proxy().callRemote('complex')
        d.addCallback(lambda result: result._asdict())
        d.addCallback(self.assertEquals, {"a": ["b", "c", 12, []], "D": "foo"})
        dl.append(d)

        # We now return to our regularly scheduled program, already in progress.
        return defer.DeferredList(dl, fireOnOneErrback=True)
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def testCooperation(self):
        L = []
        def myiter(things):
            for th in things:
                L.append(th)
                yield None

        groupsOfThings = ['abc', (1, 2, 3), 'def', (4, 5, 6)]

        c = task.Cooperator()
        tasks = []
        for stuff in groupsOfThings:
            tasks.append(c.coiterate(myiter(stuff)))

        return defer.DeferredList(tasks).addCallback(
            lambda ign: self.assertEquals(tuple(L), sum(zip(*groupsOfThings), ())))
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def testNotLoggedInReply(self):
        """When not logged in, all commands other than USER and PASS should
        get NOT_LOGGED_IN errors.
        """
        commandList = ['CDUP', 'CWD', 'LIST', 'MODE', 'PASV',
                       'PWD', 'RETR', 'STRU', 'SYST', 'TYPE']

        # Issue commands, check responses
        def checkResponse(exception):
            failureResponseLines = exception.args[0]
            self.failUnless(failureResponseLines[-1].startswith("530"),
                            "Response didn't start with 530: %r"
                            % (failureResponseLines[-1],))
        deferreds = []
        for command in commandList:
            deferred = self.client.queueStringCommand(command)
            self.assertFailure(deferred, ftp.CommandFailed)
            deferred.addCallback(checkResponse)
            deferreds.append(deferred)
        return defer.DeferredList(deferreds, fireOnOneErrback=True)
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def setUp(self):
        from twisted.internet import reactor
        self.serverFactory = protocol.ServerFactory()
        self.serverFactory.protocol = self.serverProto
        self.clientFactory = protocol.ClientFactory()
        self.clientFactory.protocol = self.clientProto
        self.clientFactory.onMade = defer.Deferred()
        self.serverFactory.onMade = defer.Deferred()
        self.serverPort = reactor.listenTCP(0, self.serverFactory)
        self.clientConn = reactor.connectTCP(
            '127.0.0.1', self.serverPort.getHost().port,
            self.clientFactory)
        def getProtos(rlst):
            self.cli = self.clientFactory.theProto
            self.svr = self.serverFactory.theProto
        dl = defer.DeferredList([self.clientFactory.onMade,
                                 self.serverFactory.onMade])
        return dl.addCallback(getProtos)
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def testDeferredListWithAlreadyFiredDeferreds(self):
        # Create some deferreds, and err one, call the other
        d1 = defer.Deferred()
        d2 = defer.Deferred()
        d1.errback(GenericError('Bang'))
        d2.callback(2)

        # *Then* build the DeferredList
        dl = defer.DeferredList([d1, d2])

        result = []
        dl.addCallback(result.append)

        self.failUnlessEqual(1, len(result))

        d1.addErrback(lambda e: None)  # Swallow error
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def test_concurrentRetrieves(self):
        """
        Issue three retrieve calls immediately without waiting for any to
        succeed and make sure they all do succeed eventually.
        """
        p, t = setUp()
        messages = [
            p.retrieve(i).addCallback(
                self.assertEquals,
                ["First line of %d." % (i + 1,),
                 "Second line of %d." % (i + 1,)])
            for i
            in range(3)]

        for i in range(1, 4):
            self.assertEquals(t.value(), "RETR %d\r\n" % (i,))
            t.clear()
            p.dataReceived("+OK 2 lines on the way\r\n")
            p.dataReceived("First line of %d.\r\n" % (i,))
            p.dataReceived("Second line of %d.\r\n" % (i,))
            self.assertEquals(t.value(), "")
            p.dataReceived(".\r\n")

        return defer.DeferredList(messages, fireOnOneErrback=True)
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def testMultipleSimultaneousCreateSameAbout(self):
        deferreds = []
        for _ in range(100):
            deferreds.append(self.createObject(about='xx'))
        results = yield defer.DeferredList(deferreds, consumeErrors=True)
        failed = False
        objectId = None
        for result in results:
            if result[0]:
                if objectId is None:
                    objectId = result[1]
                else:
                    self.assertEqual(objectId, result[1])
            else:
                failed = True
                log.err(result[1])
        if failed:
            self.fail()
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def testMultipleSimultaneousUpdateSameAbout(self):
        aboutValue = 'yy'
        objectId = yield self.createObject(about=aboutValue)
        deferreds = []
        for _ in range(100):
            deferreds.append(self.createObject(about=aboutValue))
        results = yield defer.DeferredList(deferreds, consumeErrors=True)
        failed = False
        for result in results:
            if result[0]:
                self.assertEqual(objectId, result[1])
            else:
                failed = True
                log.err(result[1])
        if failed:
            self.fail()
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def testMultipleSimultaneousCreates(self):
        path = 'fluiddb/testing/test1'
        objectId = yield self.createObject()
        deferreds = []
        try:
            for value in (3, None, True, 5.4, False, 'hey', ['a', 'b']):
                deferreds.append(self.setTagValue(path, objectId, value))
            results = yield defer.DeferredList(deferreds, consumeErrors=True)
            failed = False
            for result in results:
                if not result[0]:
                    failed = True
                    log.err(result[1])
            if failed:
                self.fail()
        finally:
            yield self.deleteTagValue(path, objectId)
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def testMultipleSimultaneousUpdates(self):
        path = 'fluiddb/testing/test1'
        objectId = yield self.createObject()
        yield self.setTagValue(path, objectId, 5)
        deferreds = []
        try:
            for _ in range(10):
                for value in (7, True, None, 4.8, False, 'hi', ['aa', 'bb']):
                    deferreds.append(self.setTagValue(path, objectId, value))
            results = yield defer.DeferredList(deferreds, consumeErrors=True)
            failed = False
            for result in results:
                if not result[0]:
                    failed = True
                    log.err(result[1])
            if failed:
                self.fail()
        finally:
            yield self.deleteTagValue(path, objectId)
项目:deb-python-txaio    作者:openstack    | 项目源码 | 文件源码
def gather(self, futures, consume_exceptions=True):
        def completed(res):
            rtn = []
            for (ok, value) in res:
                rtn.append(value)
                if not ok and not consume_exceptions:
                    value.raiseException()
            return rtn

        # XXX if consume_exceptions is False in asyncio.gather(), it will
        # abort on the first raised exception -- should we set
        # fireOnOneErrback=True (if consume_exceptions=False?) -- but then
        # we'll have to wrap the errback() to extract the "real" failure
        # from the FirstError that gets thrown if you set that ...

        dl = DeferredList(list(futures), consumeErrors=consume_exceptions)
        # we unpack the (ok, value) tuples into just a list of values, so
        # that the callback() gets the same value in asyncio and Twisted.
        add_callbacks(dl, completed, None)
        return dl
项目:bwscanner    作者:TheTorProject    | 项目源码 | 文件源码
def run_scan(self):
        all_done = defer.Deferred()
        if self.scan_continuous:
            all_done.addCallback(lambda ign: self.run_scan())
        self.circuits = TwoHop(self.state,
            partitions=self.partitions, this_partition=self.this_partition)

        def scan_over_next_circuit():
            try:
                self.fetch(self.circuits.next())
            except StopIteration:
                # All circuit measurement tasks have been setup. Now wait for
                # all tasks to complete before writing results, and firing
                # the all_done deferred.
                task_list = defer.DeferredList(self.tasks)
                task_list.addCallback(lambda _: self.result_sink.end_flush())
                task_list.chainDeferred(all_done)
            else:
                # We have circuits left, schedule scan on the next circuit
                self.clock.callLater(self.circuit_launch_delay,
                                     scan_over_next_circuit)

        # Scan the first circuit
        self.clock.callLater(0, scan_over_next_circuit)
        return all_done
项目:bwscanner    作者:TheTorProject    | 项目源码 | 文件源码
def run_scan(state):
    circuits = ExitScan(state)
    url = 'https://check.torproject.org'
    outfile = open("exit-addresses.%s.json" % datetime.datetime.utcnow().isoformat(), 'w+')
    all_tasks_done = defer.Deferred()
    tasks = []
    def pop(circuits):
        try:
            tasks.append(task.deferLater(
                reactor, 0, fetch, circuits.next(), url, state))
            reactor.callLater(.2, pop, circuits)
        except StopIteration:
            results = defer.DeferredList(tasks)
            results.addCallback(save_results, outfile)\
                   .addCallback(lambda _: outfile.close)\
                   .chainDeferred(all_tasks_done)

    reactor.callLater(0, pop, circuits)
    return all_tasks_done
项目:barium    作者:barium-project    | 项目源码 | 文件源码
def refreshDeviceLists(self):
        """Ask all GPIB bus servers for their available GPIB devices."""
        servers = [s for n, s in self.client.servers.items()
                     if (('GPIB Bus' in n) or ('gpib_bus' in n)) and \
                        (('List Devices' in s.settings) or \
                         ('list_devices' in s.settings))]
        serverNames = [s.name for s in servers]
        print 'Pinging servers:', serverNames
        resp = yield DeferredList([s.list_devices() for s in servers])
        for serverName, (success, addrs) in zip(serverNames, resp):
            if not success:
                print 'Failed to get device list for:', serverName
            else:
                print 'Server %s has devices: %s' % (serverName, addrs)
                for addr in addrs:
                    self.gpib_device_connect(serverName, addr)
项目:nav    作者:UNINETT    | 项目源码 | 文件源码
def resolve(self, names):
        """Resolves DNS names in parallel"""
        self._finished = False
        self.results = defaultdict(list)

        deferred_list = []
        for name in names:
            for deferred in self.lookup(name):
                deferred.addCallback(self._extract_records, name)
                deferred.addErrback(self._errback, name)
                deferred_list.append(deferred)

        deferred_list = defer.DeferredList(deferred_list)
        deferred_list.addCallback(self._parse_result)
        deferred_list.addCallback(self._finish)

        while not self._finished:
            reactor.iterate()
        # Although the results are in at this point, we may need an extra
        # iteration to ensure the resolver library closes its UDP sockets
        reactor.iterate()

        return dict(self.results)
项目:nav    作者:UNINETT    | 项目源码 | 文件源码
def collect(self, collectors):
        """Collects timestamp results in parallel, using a DeferredList.

        :param collectors: A list of deferreds to wait for - the deferreds
                           should return integer results.

        """
        result = yield defer.DeferredList([
                self.snmpv2mib.get_timestamp_and_uptime()
                ] + list(collectors))
        tup = []
        for success, value in result:
            if success:
                tup.append(value)
            else:
                value.raiseException()
        self.collected_times = tuple(tup)
        defer.returnValue(self.collected_times)

    # We must ignore pickle load failures by catching the Exception base class
    # pylint: disable=W0703
项目:pygear    作者:amir-khakshour    | 项目源码 | 文件源码
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):
            log.err(failure, "Error caught on signal handler: %s" % recv)
        return failure

    dont_log = named.pop('dont_log', 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
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def logOn(self, chatui):
        """
        @returns: this breaks with L{interfaces.IAccount}
        @returntype: DeferredList of L{interfaces.IClient}s
        """
        # Overriding basesupport's implementation on account of the
        # fact that _startLogOn tends to return a deferredList rather
        # than a simple Deferred, and we need to do registerAccountClient.
        if (not self._isConnecting) and (not self._isOnline):
            self._isConnecting = 1
            d = self._startLogOn(chatui)
            d.addErrback(self._loginFailed)
            def registerMany(results):
                for success, result in results:
                    if success:
                        chatui.registerAccountClient(result)
                        self._cb_logOn(result)
                    else:
                        log.err(result)
            d.addCallback(registerMany)
            return d
        else:
            raise error.ConnectionError("Connection in progress")
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def _cbGetFileSize(self, attrs, rf, lf):
        if not stat.S_ISREG(attrs['permissions']):
            rf.close()
            lf.close()
            return "Can't get non-regular file: %s" % rf.name
        rf.size = attrs['size']
        bufferSize = self.client.transport.conn.options['buffersize']
        numRequests = self.client.transport.conn.options['requests']
        rf.total = 0.0
        dList = []
        chunks = []
        startTime = self.reactor.seconds()
        for i in range(numRequests):
            d = self._cbGetRead('', rf, lf, chunks, 0, bufferSize, startTime)
            dList.append(d)
        dl = defer.DeferredList(dList, fireOnOneErrback=1)
        dl.addCallback(self._cbGetDone, rf, lf)
        return dl
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def testResults(self):
        inputOutput = [
            ("add", (2, 3), 5),
            ("defer", ("a",), "a"),
            ("dict", ({"a": 1}, "a"), 1),
            ("triple", ("a", 1), ["a", 1, None])]

        dl = []
        for meth, args, outp in inputOutput:
            d = self.proxy().callRemote(meth, *args)
            d.addCallback(self.assertEqual, outp)
            dl.append(d)

        # SOAPpy kinda blows.
        d = self.proxy().callRemote('complex')
        d.addCallback(lambda result: result._asdict())
        d.addCallback(self.assertEqual, {"a": ["b", "c", 12, []], "D": "foo"})
        dl.append(d)

        # We now return to our regularly scheduled program, already in progress.
        return defer.DeferredList(dl, fireOnOneErrback=True)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def tearDown(self):
        if self.agent:
            # clean up connections for twisted.web.client.Agent test.
            self.agent.closeCachedConnections()
            self.agent = None

        # If the test indicated it might leave some server-side connections
        # around, clean them up.
        connections = list(self.wrapper.protocols.keys())
        # If there are fewer server-side connections than requested,
        # that's okay.  Some might have noticed that the client closed
        # the connection and cleaned up after themselves.
        for n in range(min(len(connections), self.cleanupServerConnections)):
            proto = connections.pop()
            msg("Closing %r" % (proto,))
            proto.transport.abortConnection()
        d = self.port.stopListening()

        return defer.DeferredList([waitUntilAllDisconnected(
            reactor, list(self.wrapper.protocols.keys())), d])
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def testCooperation(self):
        L = []
        def myiter(things):
            for th in things:
                L.append(th)
                yield None

        groupsOfThings = ['abc', (1, 2, 3), 'def', (4, 5, 6)]

        c = task.Cooperator()
        tasks = []
        for stuff in groupsOfThings:
            tasks.append(c.coiterate(myiter(stuff)))

        return defer.DeferredList(tasks).addCallback(
            lambda ign: self.assertEqual(tuple(L), sum(zip(*groupsOfThings), ())))
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_callFromThread(self):
        """
        Test callFromThread functionality: from the main thread, and from
        another thread.
        """
        def cb(ign):
            firedByReactorThread = defer.Deferred()
            firedByOtherThread = defer.Deferred()

            def threadedFunc():
                reactor.callFromThread(firedByOtherThread.callback, None)

            reactor.callInThread(threadedFunc)
            reactor.callFromThread(firedByReactorThread.callback, None)

            return defer.DeferredList(
                [firedByReactorThread, firedByOtherThread],
                fireOnOneErrback=True)
        return self._waitForThread().addCallback(cb)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_connectionLost(self):
        """
        When disconnection occurs while commands are still outstanding, the
        commands fail.
        """
        d1 = self.proto.get(b"foo")
        d2 = self.proto.get(b"bar")
        self.transport.loseConnection()
        done = DeferredList([d1, d2], consumeErrors=True)

        def checkFailures(results):
            for success, result in results:
                self.assertFalse(success)
                result.trap(ConnectionDone)

        return done.addCallback(checkFailures)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def testDeferredListWithAlreadyFiredDeferreds(self):
        # Create some deferreds, and err one, call the other
        d1 = defer.Deferred()
        d2 = defer.Deferred()
        d1.errback(GenericError('Bang'))
        d2.callback(2)

        # *Then* build the DeferredList
        dl = defer.DeferredList([d1, d2])

        result = []
        dl.addCallback(result.append)

        self.assertEqual(1, len(result))

        d1.addErrback(lambda e: None)  # Swallow error
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_cancelDeferredListCallback(self):
        """
        When cancelling an unfired L{defer.DeferredList} without the
        C{fireOnOneCallback} and C{fireOnOneErrback} flags set, the
        L{defer.DeferredList} will be callback with a C{list} of
        (success, result) C{tuple}s.
        """
        deferredOne = defer.Deferred(fakeCallbackCanceller)
        deferredTwo = defer.Deferred()
        deferredList = defer.DeferredList([deferredOne, deferredTwo])
        deferredList.cancel()
        self.failureResultOf(deferredTwo, defer.CancelledError)
        result = self.successResultOf(deferredList)
        self.assertTrue(result[0][0])
        self.assertEqual(result[0][1], "Callback Result")
        self.assertFalse(result[1][0])
        self.assertTrue(result[1][1].check(defer.CancelledError))
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_cancelDeferredListWithFireOnOneErrback(self):
        """
        When cancelling an unfired L{defer.DeferredList} with the flag
        C{fireOnOneErrback} set, cancel every L{defer.Deferred} in the list.
        """
        deferredOne = defer.Deferred()
        deferredTwo = defer.Deferred()
        deferredList = defer.DeferredList([deferredOne, deferredTwo],
                                          fireOnOneErrback=True)
        deferredList.cancel()
        self.failureResultOf(deferredOne, defer.CancelledError)
        self.failureResultOf(deferredTwo, defer.CancelledError)
        deferredListFailure = self.failureResultOf(deferredList,
                                                   defer.FirstError)
        firstError = deferredListFailure.value
        self.assertTrue(firstError.subFailure.check(defer.CancelledError))
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback(self):
        """
        When cancelling an unfired L{defer.DeferredList} with the flag
        C{fireOnOneErrback} set, if all the L{defer.Deferred} callbacks
        in its canceller, the L{defer.DeferredList} will callback with a
        C{list} of (success, result) C{tuple}s.
        """
        deferredOne = defer.Deferred(fakeCallbackCanceller)
        deferredTwo = defer.Deferred(fakeCallbackCanceller)
        deferredList = defer.DeferredList([deferredOne, deferredTwo],
                                          fireOnOneErrback=True)
        deferredList.cancel()
        result = self.successResultOf(deferredList)
        self.assertTrue(result[0][0])
        self.assertEqual(result[0][1], "Callback Result")
        self.assertTrue(result[1][0])
        self.assertEqual(result[1][1], "Callback Result")
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_cancelDeferredListWithException(self):
        """
        Cancelling a L{defer.DeferredList} will cancel every L{defer.Deferred}
        in the list even exceptions raised from the C{cancel} method of the
        L{defer.Deferred}s.
        """
        def cancellerRaisesException(deferred):
            """
            A L{defer.Deferred} canceller that raises an exception.

            @param deferred: The cancelled L{defer.Deferred}.
            """
            raise RuntimeError("test")
        deferredOne = defer.Deferred(cancellerRaisesException)
        deferredTwo = defer.Deferred()
        deferredList = defer.DeferredList([deferredOne, deferredTwo])
        deferredList.cancel()
        self.failureResultOf(deferredTwo, defer.CancelledError)
        errors = self.flushLoggedErrors(RuntimeError)
        self.assertEqual(len(errors), 1)
项目:zenchmarks    作者:squeaky-pl    | 项目源码 | 文件源码
def test_concurrentRetrieves(self):
        """
        Issue three retrieve calls immediately without waiting for any to
        succeed and make sure they all do succeed eventually.
        """
        p, t = setUp()
        messages = [
            p.retrieve(i).addCallback(
                self.assertEqual,
                ["First line of %d." % (i + 1,),
                 "Second line of %d." % (i + 1,)])
            for i
            in range(3)]

        for i in range(1, 4):
            self.assertEqual(t.value(), "RETR %d\r\n" % (i,))
            t.clear()
            p.dataReceived("+OK 2 lines on the way\r\n")
            p.dataReceived("First line of %d.\r\n" % (i,))
            p.dataReceived("Second line of %d.\r\n" % (i,))
            self.assertEqual(t.value(), "")
            p.dataReceived(".\r\n")

        return defer.DeferredList(messages, fireOnOneErrback=True)
项目:stethoscope    作者:Netflix    | 项目源码 | 文件源码
def get_devices_by_email(email, extensions, debug=False):
  """Returns all devices matching given email."""
  deferreds = []
  for ext in extensions:
    if hasattr(ext.obj, 'get_devices_by_email'):
      deferred = ext.obj.get_devices_by_email(email)
      deferred.addErrback(stethoscope.api.utils.check_user_not_found)
      deferred.addCallback(functools.partial(log_response, 'device',
        ext.name + " ({!s})".format(email), debug=debug))
      deferreds.append(deferred)

  deferred_list = defer.DeferredList(deferreds, consumeErrors=True)
  deferred_list.addCallback(functools.partial(stethoscope.api.utils.filter_keyed_by_status,
    ["{!s}({!s})".format(ext.name, email) for ext in extensions],
    context=sys._getframe().f_code.co_name))
  deferred_list.addCallback(lambda d: chain.from_iterable(d.values()))
  deferred_list.addCallback(list)
  return deferred_list
项目:stethoscope    作者:Netflix    | 项目源码 | 文件源码
def get_devices_by_macaddr(macaddr, extensions, debug=False):
  """Returns all devices matching given MAC address."""
  deferreds = []
  for ext in extensions:
    if hasattr(ext.obj, 'get_devices_by_macaddr'):
      deferred = ext.obj.get_devices_by_macaddr(macaddr)
      deferred.addErrback(stethoscope.api.utils.check_device_not_found)
      deferred.addCallback(functools.partial(log_response, 'device',
        ext.name + " ({!s})".format(macaddr), debug=debug))
      deferreds.append(deferred)

  deferred_list = defer.DeferredList(deferreds, consumeErrors=True)
  deferred_list.addCallback(functools.partial(stethoscope.api.utils.filter_keyed_by_status,
    ["{!s}({!s})".format(ext.name, macaddr) for ext in extensions],
    context=sys._getframe().f_code.co_name))
  deferred_list.addCallback(lambda d: chain.from_iterable(d.values()))
  deferred_list.addCallback(list)
  return deferred_list
项目:ooniprobe-debian    作者:TheTorProject    | 项目源码 | 文件源码
def _test_tcp_connect(self, consistent_addresses):
        for key, addresses in consistent_addresses.items():
            if key == 'stun':
                # XXX we currently don't test stun
                continue

            dl = []
            for address in addresses:
                dl.append(self._test_connect_to_port(address, 443))
            results = yield defer.DeferredList(dl, consumeErrors=True)
            tcp_blocked = False
            for success, result in results:
                if success == False:
                    tcp_blocked = True

            if tcp_blocked == True:
                log.msg("{0} server is BLOCKED based on TCP".format(key))
            if len(addresses) > 0:
                self.report['facebook_{0}_reachable'.format(key)] = not tcp_blocked
项目:ooniprobe-debian    作者:TheTorProject    | 项目源码 | 文件源码
def _test_tcp_connect(self):
        for dc_id, address in TELEGRAM_DCS:
            dl = []
            log.debug("Testing %s:443|80" % (address))
            dl.append(self._test_connect_to_port(address, 443))
            dl.append(self._test_connect_to_port(address, 80))

        results = yield defer.DeferredList(dl, consumeErrors=True)
        tcp_blocked = True
        for success, result in results:
            if success == True:
                tcp_blocked = False

        if tcp_blocked == True:
            self.report['telegram_tcp_blocking'] = True
            log.msg("Telegram servers are BLOCKED based on TCP")
        else:
            self.report['telegram_tcp_blocking'] = False
            log.msg("Telegram servers are not blocked based on TCP")
项目:ooniprobe-debian    作者:TheTorProject    | 项目源码 | 文件源码
def test_filesystem_lock_and_mutex(self):
        lock_dir = tempfile.mkdtemp()
        lock_path = os.path.join(lock_dir, 'lock')

        lock = FileSystemlockAndMutex(lock_path)

        os.symlink(str(2**30), lock_path) # that's non-existend PID for sure

        lock_count = 100
        unlock_count = 0
        dl = []
        for i in range(lock_count):
            dl.append(lock.acquire())
            if random.choice([0, 1]) == 0:
                unlock_count += 1
                lock.release()

        for i in range(lock_count - unlock_count):
            lock.release()

        yield defer.DeferredList(dl)
        self.assertFalse(lock.locked)

        shutil.rmtree(lock_dir)
项目:xr-telemetry-m2m-web    作者:cisco    | 项目源码 | 文件源码
def render_POST(self, request):
        #
        # Turn a path or show command into a set of candidate protobuf definitions.
        # If it looks like a show command, then schema-describe it, get the set of
        # paths, and run the GPB generation on each one. Otherwise, just run that on
        # the sole provided path.
        #
        path = request.args['path'][0].strip()
        if path.startswith('sh'):
            d = scrape.schema_describe(path, request.sdata)
        else:
            d = defer.succeed([path])

        def request_protobufs(paths):
            print('### PROTOBUF PATHS = {}'.format(paths))
            ds = []
            for path in reversed(paths):
                path = re.sub('\(.*?\)', '', path)
                ds.append(request.sdata.api.cli_exec(
                            'run telemetry_generate_gpb "{}"'.format(path)))
            return defer.DeferredList(ds)
        d.addCallback(request_protobufs)

        def get_protobufs(replies):
            line = '-' * 77
            sep = '\n//\n// ' + line + '\n//\n\n'
            text = sep.join([reply[1]['result'] for reply in replies])
            request.sdata.set_text('#protobuf_result', text)
            request.sdata.add_to_push_queue('stop_current_spinner')
            request.sdata.highlight('#protobuf_result')
        d.addCallback(get_protobufs)

        request.setHeader('Content-Type', 'application/json')
        return '{}'
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def _cbIdent(self, ident, chatui):
        if not ident:
            print 'falsely identified.'
            return self._ebConnected(Failure(Exception("username or password incorrect")))
        print 'Identified!'
        dl = []
        for handlerClass, sname, pname in self.services:
            d = defer.Deferred()
            dl.append(d)
            handler = handlerClass(self, sname, pname, chatui, d)
            ident.callRemote('attach', sname, pname, handler).addCallback(handler.connected)
        return defer.DeferredList(dl)