How to use the flower.core.channel function in flower

To help you get started, we’ve selected a few flower examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github benoitc / flower / test / test_channel.py View on Github external
def test_balance_zero(self):
        ch=core.channel()
        assert ch.balance == 0
github benoitc / flower / test / test_channel.py View on Github external
dprint('twt_S ==== 3')

        def wait_two(X, Y, Ret_chan):
            Barrier = core.channel()
            core.tasklet(sleep)(X, Barrier)
            core.tasklet(sleep)(Y, Barrier)
            dprint('twt_W ==== 1')
            ret = Barrier.receive()
            dprint('twt_W ==== 2')
            if ret[0] == X:
                Ret_chan.send((1, ret[1]))
            else:
                Ret_chan.send((2, ret[1]))
            dprint('twt_W ==== 3')

        X = core.channel()
        Y = core.channel()
        Ret_chan = core.channel()

        core.tasklet(wait_two)(X, Y, Ret_chan)

        dprint('twt ==== 1')
        Y.send(42)

        dprint('twt ==== 2')
        X.send(42)
        dprint('twt ==== 3')
        value = Ret_chan.receive()
        dprint('twt ==== 4')
        assert value == (2, 42)
github benoitc / flower / test / test_channel.py View on Github external
def test_async_channel(self):
        c = core.channel(100)

        unblocked_sent = 0
        for i in range(100):
            c.send(True)
            unblocked_sent += 1

        assert unblocked_sent == 100
        assert c.balance == 100

        unblocked_recv = []
        for i in range(100):
            unblocked_recv.append(c.receive())

        assert len(unblocked_recv) == 100
github benoitc / flower / test / test_channel.py View on Github external
def test_send_sequence(self):
        res = []
        lst = [1,2,3,4,5,6,None]
        iterable = iter(lst)
        chan = core.channel()
        def f(chan):
            r = chan.receive()
            while r:
                res.append(r)
                r = chan.receive()

        t = core.tasklet(f)(chan)
        chan.send_sequence(iterable)
        assert res == [1,2,3,4,5,6]
github benoitc / flower / test / test_channel.py View on Github external
def test_send_exception(self):
        def exp_sender(chan):
            chan.send_exception(Exception, 'test')

        def exp_recv(chan):
            try:
                val = chan.receive()
            except Exception as exp:
                assert exp.__class__ is Exception
                assert str(exp) == 'test'

        chan = core.channel()
        t1 = core.tasklet(exp_recv)(chan)
        t2 = core.tasklet(exp_sender)(chan)
        core.run()
github benoitc / flower / test / test_channel.py View on Github external
def nest(X, Y):
            X2, Y2 = core.channel(), core.channel()
            t = core.tasklet(pipe)(X2, Y2)
            dprint('tnp_N ==== 1')
            X_Val = X.receive()
            dprint('tnp_N ==== 2')
            X2.send(X_Val)
            dprint('tnp_N ==== 3')
            Y2_Val = Y2.receive()
            dprint('tnp_N ==== 4')
            Y.send(Y2_Val)
            dprint('tnp_N ==== 5')
github benoitc / flower / test / test_channel.py View on Github external
def wait_two(X, Y, Ret_chan):
            Barrier = core.channel()
            core.tasklet(sleep)(X, Barrier)
            core.tasklet(sleep)(Y, Barrier)
            dprint('twt_W ==== 1')
            ret = Barrier.receive()
            dprint('twt_W ==== 2')
            if ret[0] == X:
                Ret_chan.send((1, ret[1]))
            else:
                Ret_chan.send((2, ret[1]))
            dprint('twt_W ==== 3')

        X = core.channel()
        Y = core.channel()
        Ret_chan = core.channel()

        core.tasklet(wait_two)(X, Y, Ret_chan)

        dprint('twt ==== 1')
        Y.send(42)

        dprint('twt ==== 2')
        X.send(42)
        dprint('twt ==== 3')
        value = Ret_chan.receive()
        dprint('twt ==== 4')
        assert value == (2, 42)
github benoitc / flower / test / test_channel.py View on Github external
def test_async_with_blocking_channel(self):

        c = core.channel(10)

        unblocked_sent = 0
        for i in range(10):
            c.send(True)
            unblocked_sent += 1

        assert unblocked_sent == 10
        assert c.balance == 10

        r_list = []
        def f():
            start = time.time()
            c.send(True)
            r_list.append(start)

        core.tasklet(f)()
github benoitc / flower / flower / net / sock.py View on Github external
def __init__(self, client, laddr, addr):
        # set connection info
        self.client = client
        self.client.setblocking(0)
        self.timeout = socket.getdefaulttimeout()
        self.laddr = laddr
        self.addr = addr

        # utilies used to fetch & send ata
        self.cr = channel() # channel keeping readers waiters
        self.cw = channel() # channel keeping writers waiters
        self.queue = deque() # queue of readable data
        self.uv = uv_server()
        self.rpoller = None
        self.wpoller = None
        self._lock = threading.RLock()
        self.ncr = 0 # reader refcount
        self.ncw = 0 # writer refcount

        self.closing = False
github benoitc / flower / flower / actor.py View on Github external
def __init__(self):
        self.messages = deque()
        self.channel = core.channel()
        self._lock = threading.RLock()