Пример #1
0
    def mass(self, flow, out, user, channel, *args):
        """
		\x02mass\x02 <command> [<arguments>]
		Execute the command with the specified arguments mapped on every piped list item
		The arguments string must contain '?' exactly once, which will hold the iterated items
		"""
        d = Deferred()
        command = " ".join(args).replace("=>", "->")
        for item in flow:
            d.chainDeferred(self._handle(user, channel, command.replace("?", item), True))
        d.callback(None)
        return d
Пример #2
0
    def mass(self, flow, out, user, channel, *args):
        '''
		\x02mass\x02 <command> [<arguments>]
		Execute the command with the specified arguments mapped on every piped list item
		The arguments string must contain '?' exactly once, which will hold the iterated items
		'''
        d = Deferred()
        command = ' '.join(args).replace('=>', '->')
        for item in flow:
            d.chainDeferred(
                self._handle(user, channel, command.replace('?', item), True))
        d.callback(None)
        return d
Пример #3
0
 def _call_method(self, methodDescriptor, rpcController, request,
                  responseClass, done):
     self.id += 1
     d = Deferred()
     d.addCallback(self.unserialize_response, responseClass, rpcController)
     d.chainDeferred(done)
     self._pending[self.id] = d
     rpc = Rpc()
     rpcRequest = rpc.request.add()
     rpcRequest.method = methodDescriptor.containing_service.name + '.' + methodDescriptor.name
     rpcRequest.serialized_request = request.SerializeToString()
     rpcRequest.id = self.id
     return rpc
Пример #4
0
    def test_task_queue(self):
        """Test the RSSFeedRunQueue
        Test that the jobs are not run in main thread and that the jobs are run sequentially
        """
        main_thread = threading.current_thread()
        runtime_dict = {}

        def test_run(id):
            # Save the start and end time of the job
            runtime_dict[id] = {"start": yarss2.util.common.get_current_date()}
            # Test that the job is not run in main thread
            self.assertNotEquals(threading.current_thread(), main_thread)
            runtime_dict[id]["end"] = yarss2.util.common.get_current_date()
            return id

        result_callback_ids = []

        def result_callback(id):
            result_callback_ids.append(id)

        ids = [str(i) for i in range(10)]
        taskq = RSSFeedRunQueue()
        # Start id_count jobs
        for id in ids:
            d = taskq.push(test_run, str(id))
            d.addCallback(result_callback)

        def verify_times(arg):
            """Verify that start time is later than the previous job's end time"""
            tmp_date = yarss2.util.common.get_default_date()
            for id in range(len(ids)):
                runtime_dict[str(id)]
                self.assertTrue(tmp_date < runtime_dict[str(id)]["start"])
                tmp_date = runtime_dict[str(id)]["end"]

        d_verify = Deferred()
        d_verify.addCallback(verify_times)
        # Add verify_times to the last added deferred
        d.chainDeferred(d_verify)

        def verify_callback_results(args):
            self.assertEquals(len(ids), len(result_callback_ids))
            for i in range(len(ids)):
                self.assertEquals(ids[i], result_callback_ids[i])

        d_verify_callback = Deferred()
        d_verify_callback.addCallback(verify_callback_results)
        # Add verify_callback_results to the deferred chain
        d_verify.chainDeferred(d_verify_callback)
        return d_verify
class ChunkedStreamProducer(object):
    interface.implements(IBodyProducer)

    def __init__(self, started, length=UNKNOWN_LENGTH):
        self.length = length
        self.consumer = None
        self.started = Deferred()
        self.finished = Deferred()

    def startProducing(self, consumer):
        print "ChunkedStreamProducer: START PRODUCING"
        self.consumer = consumer
        self.started.callback(None)
        return self.finished

    def _send(self, result, data):
        print "ChunkedStreamProducer: _SEND"
        return self.consumer.write(data)

    def send(self, data):
        print "ChunkedStreamProducer: SEND"
        d = Deferred()
        self.started.chainDeferred(d)
        d.addCallback(self._send, data)
        return d

    def finish(self):
        def _finish(result):
            self.finished.callback(None)
            return None

        d = Deferred()
        self.started.chainDeferred(d)
        d.addCallback(_finish)
        return d

    def pauseProducing(self):
        print "pause"
        pass

    def stopProducing(self):
        print "STOP"
        pass
Пример #6
0
def test_backend_components():
    testDone = getMainDeferred()

    clientDone = Deferred()
    backend = ClientBackend(clientDone)

    # Give it some components.
    graphics_dummy = DummyComponent(backend.graphicsMessage)
    stdio_dummy    = DummyComponent(backend.stdioMessage)
    network_dummy  = DummyComponent(backend.networkMessage)

    # Tell it they're all ready. A fancier test might randomize the order of
    # these and insert some delays, but this is just a simple smoketest.
    backend.stdioReady(stdio_dummy)
    backend.networkReady(network_dummy)
    backend.graphicsInterfaceReady(graphics_dummy)

    def sendMessages():
        network_dummy.sendMessage(YourIdIs(42).serialize())
        graphics_dummy.sendMessage(RequestQuit().serialize())

    def finalChecks(x):
        # Check that the YourIdIs(42) was successfully forwarded to the
        # graphics interface.
        gotId = False
        for msgData in graphics_dummy.messageLog:
            msg = deserializeMessage(msgData)
            if isinstance(msg, YourIdIs):
                assert msg.playerId == 42
                gotId = True
        assert gotId

    clientDone.addCallback(finalChecks)
    clientDone.chainDeferred(testDone)

    # Make sure to chain the main Deferred's errback to that of this
    # deferLater, so that if something goes wrong in sendMessage then it will
    # be reported (rather than silently dropped).
    d = task.deferLater(reactor, 0.05, sendMessages)
    d.addErrback(testDone.errback)

    return testDone
class ChunkedStreamProducer(object):
    interface.implements(IBodyProducer)

    def __init__(self, started, length=UNKNOWN_LENGTH):
        self.length = length
        self.consumer = None
        self.started = Deferred()
        self.finished = Deferred()

    def startProducing(self, consumer):
        print "ChunkedStreamProducer: START PRODUCING"
        self.consumer = consumer
        self.started.callback(None)
        return self.finished

    def _send(self, result, data):
        print "ChunkedStreamProducer: _SEND"
        return self.consumer.write(data)

    def send(self, data):
        print "ChunkedStreamProducer: SEND"
        d = Deferred()
        self.started.chainDeferred(d)
        d.addCallback(self._send, data)
        return d

    def finish(self):
        def _finish(result):
            self.finished.callback(None)
            return None
        d = Deferred()
        self.started.chainDeferred(d)
        d.addCallback(_finish)
        return d

    def pauseProducing(self):
        print "pause"
        pass

    def stopProducing(self):
        print "STOP"
        pass
Пример #8
0
def test_backend_components():
    testDone = getMainDeferred()

    clientDone = Deferred()
    backend = ClientBackend(clientDone)

    # Give it some components.
    graphics_dummy = DummyComponent(backend.graphicsMessage)
    stdio_dummy = DummyComponent(backend.stdioMessage)
    network_dummy = DummyComponent(backend.networkMessage)

    # Tell it they're all ready. A fancier test might randomize the order of
    # these and insert some delays, but this is just a simple smoketest.
    backend.stdioReady(stdio_dummy)
    backend.networkReady(network_dummy)
    backend.graphicsInterfaceReady(graphics_dummy)

    def sendMessages():
        network_dummy.sendMessage(YourIdIs(42).serialize())
        graphics_dummy.sendMessage(RequestQuit().serialize())

    def finalChecks(x):
        # Check that the YourIdIs(42) was successfully forwarded to the
        # graphics interface.
        gotId = False
        for msgData in graphics_dummy.messageLog:
            msg = deserializeMessage(msgData)
            if isinstance(msg, YourIdIs):
                assert msg.playerId == 42
                gotId = True
        assert gotId

    clientDone.addCallback(finalChecks)
    clientDone.chainDeferred(testDone)

    # Make sure to chain the main Deferred's errback to that of this
    # deferLater, so that if something goes wrong in sendMessage then it will
    # be reported (rather than silently dropped).
    d = task.deferLater(reactor, 0.05, sendMessages)
    d.addErrback(testDone.errback)

    return testDone
    def run(self):
        rollingresult = {}
        deferredchain = Deferred()
        print("Setting up defered chain")
        while self.url_list:
            if self.batch_size < len(self.url_list):
                batch_actual = self.batch_size
                terminator = pause_gather(self.wait_time, rollingresult)
            else:
                batch_actual = len(self.url_list)
                terminator = cleanup
            #thisbatch = [self.url_list.pop() for _ in range(batch_actual)]
            thisbatch = self.url_list[-batch_actual:]
            print("built: \n\t-" + "\n\t-".join(thisbatch))
            PFR_list = [self.processor(url, self.session) for url in thisbatch]
            delta = tin.defer.gatherResults(PFR_list)
            delta.addCallback(terminator)
            deferredchain.chainDeferred(delta)
        self.downloadedvalues = rollingresult

        tin.reactor.run()
        return rollingresult
Пример #10
0
    def request_chunk(self, x, z):
        """
        Request a ``Chunk`` to be delivered later.

        :returns: Deferred that will be called with the Chunk
        """

        if not async:
            return deferLater(reactor, 0.000001, self.factory.world.load_chunk,
                x, z)

        if (x, z) in self.chunk_cache:
            return succeed(self.chunk_cache[x, z])
        elif (x, z) in self.dirty_chunk_cache:
            return succeed(self.dirty_chunk_cache[x, z])
        elif (x, z) in self._pending_chunks:
            # Rig up another Deferred and wrap it up in a to-go box.
            d = Deferred()
            self._pending_chunks[x, z].chainDeferred(d)
            return d

        chunk = Chunk(x, z)

        first, second, filename = names_for_chunk(x, z)
        f = self.folder.child(first).child(second)
        if not f.exists():
            f.makedirs()
        f = f.child(filename)
        if f.exists() and f.getsize():
            chunk.load_from_tag(read_from_file(f.open("r")))

        if chunk.populated:
            self.chunk_cache[x, z] = chunk
            return succeed(chunk)

        d = deferToAMPProcess(MakeChunk, x=x, z=z, seed=self.seed,
            generators=configuration.getlist("bravo", "generators"))
        self._pending_chunks[x, z] = d

        def pp(kwargs):
            chunk.blocks = fromstring(kwargs["blocks"],
                dtype=uint8).reshape(chunk.blocks.shape)
            chunk.heightmap = fromstring(kwargs["heightmap"],
                dtype=uint8).reshape(chunk.heightmap.shape)
            chunk.metadata = fromstring(kwargs["metadata"],
                dtype=uint8).reshape(chunk.metadata.shape)
            chunk.skylight = fromstring(kwargs["skylight"],
                dtype=uint8).reshape(chunk.skylight.shape)
            chunk.blocklight = fromstring(kwargs["blocklight"],
                dtype=uint8).reshape(chunk.blocklight.shape)

            chunk.populated = True
            chunk.dirty = True

            # Apply the current season to the chunk.
            if self.season:
                self.season.transform(chunk)

            # Since this chunk hasn't been given to any player yet, there's no
            # conceivable way that any meaningful damage has been accumulated;
            # anybody loading any part of this chunk will want the entire thing.
            # Thus, it should start out undamaged.
            chunk.clear_damage()

            self.dirty_chunk_cache[x, z] = chunk
            del self._pending_chunks[x, z]

            return chunk

        # Set up callbacks.
        d.addCallback(pp)
        # Multiple people might be subscribed to this pending callback. We're
        # going to keep it for ourselves and fork off another Deferred for our
        # caller.
        forked = Deferred()
        d.chainDeferred(forked)
        forked.addCallback(lambda none: chunk)
        return forked
Пример #11
0
    print(d)
    return d


def error2(d):
    print('error2 two')
    print(d)
    return d


def reportCancel(fail, which):
    fail.trap(CancelledError)
    print('cancelled', which)


def print_fun(d):
    print("print_fun", d)
    return d


url = 'https://www.smzdm.com/homepage/json_more?p='
d1 = Deferred()
d1.addCallback(print1)
d1.addErrback(error1)
d2 = Deferred().addBoth(print_fun)
d2.addCallbacks(print2, error2)

d1.chainDeferred(d2)
reactor.callLater(2, d1.callback, "hey")
reactor.run()