Skip to content.

logo Stackless Python


Personal tools
Views

BroadcastChannel

last edited 4 years ago by sholloway

Example: Broadcast Channel:

    import stackless

    class BroadcastChannel(stackless.channel):
        def send(self, value, wait=False):
            result = None
            for idx in range(0, self.balance, -1):
                # there are tasklets waiting to receive
                result = stackless.channel.send(self, value)
            return result
        def send_exception(self, exc, value, wait=False):
            result = None
            for idx in range(0, self.balance, -1):
                # there are tasklets waiting to receive
                result = stackless.channel.send_exception(self, exc, value)
            return result

    def testBroadcastChannel():
        print
        print "testBroadcastChannel"
        print "--------------------"
        def recv(ch, name):
            print "Started recv<%s>" % (name,)
            print "recv<%s>: %r" % (name, ch.receive())

        ch = BroadcastChannel()

        # Essentially nonblocking on sends when there are no receivers
        ch.send("Test when empty")

        for name in "ABCDE":
            task = stackless.tasklet(recv)(ch, name)
            task.run()

        ch.send("broadcast from host")
        print

testBroadcastChannel() should print:

    testBroadcastChannel
    --------------------
    Started recv<A>
    Started recv<B>
    Started recv<C>
    Started recv<D>
    Started recv<E>
    recv<A>: 'broadcast from host'
    recv<B>: 'broadcast from host'
    recv<C>: 'broadcast from host'
    recv<D>: 'broadcast from host'
    recv<E>: 'broadcast from host'

 

Powered by Plone