示例#1
0
    def testReducerFuture(self):
        @tasklets.tasklet
        def sum_tasklet(arg):
            yield tasklets.sleep(0.01)
            raise tasklets.Return(sum(arg))

        @tasklets.tasklet
        def produce_one(i):
            yield tasklets.sleep(i * 0.01)
            raise tasklets.Return(i)

        @tasklets.tasklet
        def producer():
            for i in range(10):
                q.add_dependent(produce_one(i))
            q.complete()

        @tasklets.tasklet
        def consumer():
            total = yield q
            self.assertEqual(total, sum(range(10)))

        @tasklets.tasklet
        def foo():
            yield producer(), consumer()

        q = tasklets.ReducingFuture(sum_tasklet, batch_size=3)
        foo().get_result()
        q = tasklets.ReducingFuture(sum, batch_size=3)
        foo().get_result()
示例#2
0
    def testReducingFuture_Empty(self):
        def reducer(arg):
            self.fail()

        rf = tasklets.ReducingFuture(reducer)
        rf.complete()
        self.assertEqual(rf.get_result(), None)
示例#3
0
    def testReducingFuture_OneItem(self):
        def reducer(arg):
            self.fail()

        rf = tasklets.ReducingFuture(reducer)
        rf.putq(1)
        rf.complete()
        self.assertEqual(rf.get_result(), 1)
示例#4
0
    def testReducingFuture_ReducerException_1(self):
        def reducer(arg):
            raise ZeroDivisionError

        rf = tasklets.ReducingFuture(reducer)
        rf.putq(1)
        rf.putq(1)
        rf.complete()
        self.assertRaises(ZeroDivisionError, rf.get_result)
示例#5
0
    def testReducingFuture_ItemException(self):
        def reducer(arg):
            return sum(arg)

        rf = tasklets.ReducingFuture(reducer)
        f1 = Future()
        f1.set_exception(ZeroDivisionError())
        rf.putq(f1)
        rf.complete()
        self.assertRaises(ZeroDivisionError, rf.get_result)
示例#6
0
    def testReducingFuture_ReducerFuture_1(self):
        def reducer(arg):
            f = Future()
            f.set_result(sum(arg))
            return f

        rf = tasklets.ReducingFuture(reducer, batch_size=2)
        rf.putq(1)
        rf.putq(1)
        rf.complete()
        self.assertEqual(rf.get_result(), 2)
示例#7
0
    def testReducingFuture(self):
        def reducer(arg):
            return sum(arg)

        rf = tasklets.ReducingFuture(reducer, batch_size=10)
        for i in range(10):
            rf.putq(i)
        for i in range(10, 20):
            f = Future()
            rf.putq(f)
            f.set_result(i)
        rf.complete()
        self.assertEqual(rf.get_result(), sum(range(20)))
示例#8
0
    def testReducingFuture_ReducerFuture_2(self):
        # Weird hack to reach _internal_add_dependent() call in _mark_finished().
        def reducer(arg):
            res = sum(arg)
            if len(arg) < 3:
                f = Future()
                f.set_result(res)
                res = f
            return res

        rf = tasklets.ReducingFuture(reducer, batch_size=3)
        rf.putq(1)
        rf.putq(1)
        rf.putq(1)
        rf.putq(1)
        rf.complete()
        self.assertEqual(rf.get_result(), 4)