Exemplo n.º 1
0
    def test_multipath_batch_works(self):
        N_CALLS = [0]
        @batched()
        def fn(arg_list):
            N_CALLS[0] += 1

        def get_thing_1():
            fn(1)
            fn(2)

        def get_thing_1_sleep():
            fn(1)
            gevent.sleep(0)
            fn(2)

        def get_thing_2():
            fn(2)
            gevent.sleep(0.0001)  # "block" the greenlet for e.g. a mysql query.
            fn(1)

        def test(thing_1):
            N_CALLS[0] = 0
            g1 = spawn(thing_1)
            g2 = spawn(get_thing_2)
            g1.get()
            g2.get()
            return N_CALLS[0]

        self.assertEquals(2, spawn(test, get_thing_1).get())
        self.assertEquals(2, spawn(test, get_thing_1_sleep).get())
Exemplo n.º 2
0
 def test(thing_1):
     N_CALLS[0] = 0
     g1 = spawn(thing_1)
     g2 = spawn(get_thing_2)
     g1.get()
     g2.get()
     return N_CALLS[0]
Exemplo n.º 3
0
        def test():
            a, b = spawn(fn, 1), spawn(fn, 2)
            self.assertEquals(2, b.get())
            self.assertRaises(ValueError, a.get)

            a, b = fn(1, as_future=True), fn(2, as_future=True)
            self.assertEquals(2, b.get())
            self.assertRaises(ValueError, a.get)
Exemplo n.º 4
0
        def test():
            g1 = spawn(fn, 1)
            g2 = spawn(fn, 2)
            g1.get()
            g2.get()
            self.assertEquals(1, N_CALLS[0])

            g1, g2 = fn(1, as_future=True), fn(2, as_future=True)
            g1.wait()
            g2.wait()
            g1.get()
            g2.get()
            self.assertEquals(2, N_CALLS[0])
Exemplo n.º 5
0
    def test_greenlet(self):
        s = BatchAsyncResult()
        def fn():
            return s.get()

        def finish_it():
            s.set(1)
            gevent.sleep(0)

        tfuture = spawn(fn)
        self._do_test_future(tfuture, finish_it)
        self.assertEquals(1, tfuture.get())
Exemplo n.º 6
0
    def test_with_gevent(self):
        CALLS = []
        @batched(accepts_kwargs=False)
        def fn(arg_list):
            CALLS.append(arg_list)

        def get_thing():
            thing = spawn(fn, 1)
            gevent.spawn(fn, 2).get()
            fn(3)
            thing.get()

        def test():
            g1, g2 = spawn(get_thing), spawn(get_thing)
            g1.get()
            g2.get()

        spawn(test).get()
        # We get 3 "batches" of calls: 2 batches of [(2,)] which are the two
        # independent gevent.spawns and 1 large batch of 1s and 3s from the
        # future.
        self.assertEquals([[(2,)], [(2,)], [(1,), (1,), (3,), (3,)]],
                          CALLS)
Exemplo n.º 7
0
 def test():
     g1 = spawn(fn, 1)
     g2 = spawn(fn, 2)
     g1.get()
     g2.get()
     self.assertEquals(2, N_CALLS[0])
Exemplo n.º 8
0
 def multiply_by(value_f, m=3):
     self.assertTrue(value_f.ready())
     return spawn(lambda: value_f.get() * m)
Exemplo n.º 9
0
 def test():
     a, b = spawn(fn), spawn(fn2)
     self.assertEquals(0, N_CALLS[0])
     a.get(), b.get()
Exemplo n.º 10
0
 def test():
     a, b = spawn(fn, 1), spawn(fn, 2)
     return a.get(), b.get()
Exemplo n.º 11
0
 def test():
     g1, g2 = spawn(get_thing), spawn(get_thing)
     g1.get()
     g2.get()
Exemplo n.º 12
0
 def get_thing():
     thing = spawn(fn, 1)
     gevent.spawn(fn, 2).get()
     fn(3)
     thing.get()