コード例 #1
0
 def f_pong(ping):
     while 1:
         print("pong" + str(self.x))
         self.x = self.x - 1
         coroutine.transfer(ping)
         if self.x == 0:
             break
コード例 #2
0
    def remove_all(self, elt):
        num_removed = 0
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                num_removed += 1
                if nn == self.tail:
                    self.tail = node
                    break

                nn = nn.next  # iterate nn
                # yield
                coroutine.transfer(label="remove_all_1")
                continue

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_all_2")
        return num_removed
コード例 #3
0
    def test_ping_pong(self):

        self.x = 10
        
        def f_ping(pong):
            while 1:
                print("ping" + str(self.x))
                self.x = self.x - 1
                coroutine.transfer(pong)
                if self.x == 0:
                    break


        def f_pong(ping):
            while 1:
                print("pong" + str(self.x))
                self.x = self.x - 1
                coroutine.transfer(ping)
                if self.x == 0:
                    break

        ping = coroutine(f_ping)
        pong = coroutine(f_pong)

        ping(pong)
        pong(ping)

        # returns back when both ping and pong terminates
        coroutine.transfer(ping)
コード例 #4
0
ファイル: concurrent_list.py プロジェクト: panglong/concurrit
    def remove_all(self, elt):
        num_removed = 0
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                num_removed += 1
                if nn == self.tail:
                    self.tail = node
                    break

                nn = nn.next # iterate nn
                # yield
                coroutine.transfer(label="remove_all_1")
                continue

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_all_2")
        return num_removed
コード例 #5
0
 def foreach(self, f):
     node = self.head.next
     while node != None:
         if not node.marked:
             elt = node.elt
             f(elt)
             #yield
             coroutine.transfer(label="foreach")
         node = node.next
コード例 #6
0
ファイル: concurrent_list.py プロジェクト: panglong/concurrit
 def foreach(self, f):
     node = self.head.next
     while node != None:
         if not node.marked:
             elt = node.elt
             f(elt)
             #yield
             coroutine.transfer(label="foreach")
         node = node.next
コード例 #7
0
    def test_generator(self):
        def generator(k):
            for i in range(1,k):
                print(i)
                coroutine.transfer(None)

        c = coroutine(generator)(100)

        for i in range(1, 50):
            coroutine.transfer(c)
コード例 #8
0
    def add(self, elt, sc=[]):
        # yield
        coroutine.transfer(label="add_1")

        if self.capacity > 0 and self.size() == self.capacity:
            raise RuntimeError("Capacity is reached!")

        node = concurrent_list.node(elt, None)
        self.tail.next = node
        self.tail = node

        # yield
        coroutine.transfer(label="add_2")
コード例 #9
0
ファイル: concurrent_list.py プロジェクト: panglong/concurrit
    def add(self, elt, sc = []):
        # yield
        coroutine.transfer(label="add_1")

        if self.capacity > 0 and self.size() == self.capacity:
            raise RuntimeError("Capacity is reached!")

        node = concurrent_list.node(elt, None)
        self.tail.next = node
        self.tail = node

        # yield
        coroutine.transfer(label="add_2")
コード例 #10
0
ファイル: concurrent_list.py プロジェクト: panglong/concurrit
    def take(self):
        # block until the list is non-empty
        while self.is_empty():
            # yield
            coroutine.transfer(label="take_1")
            pass

        assert self.head != self.tail
        assert self.head.next != None
        elt = self.head.next.elt
        self.head = self.head.next
        # yield
        coroutine.transfer(label="take_2")
        return elt
コード例 #11
0
    def take(self):
        # block until the list is non-empty
        while self.is_empty():
            # yield
            coroutine.transfer(label="take_1")
            pass

        assert self.head != self.tail
        assert self.head.next != None
        elt = self.head.next.elt
        self.head = self.head.next
        # yield
        coroutine.transfer(label="take_2")
        return elt
コード例 #12
0
    def test_positive_negative(self):

        def positive(k, neg):
            for i in range(1,k):
                print(i)
                coroutine.transfer(neg)

        def negative(k):
            for i in range(1,k):
                print(-i)
                coroutine.transfer(None)

        neg = coroutine(negative)
        pos = coroutine(positive)

        neg(100)
        pos(100, neg)

#        for i in range(1, 50): # this creates a deadlock since neg returns back to pos, not here!
        coroutine.transfer(pos)
コード例 #13
0
    def remove(self, elt):
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                if nn == self.tail:
                    self.tail = node

                # yield
                coroutine.transfer(label="remove_1")
                return True

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_2")
        return False
コード例 #14
0
ファイル: concurrent_list.py プロジェクト: panglong/concurrit
    def remove(self, elt):
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                if nn == self.tail:
                    self.tail = node

                # yield
                coroutine.transfer(label="remove_1")
                return True

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_2")
        return False
コード例 #15
0
 def generator(k):
     for i in range(1,k):
         print(i)
         coroutine.transfer(None)
コード例 #16
0
 def positive(k, neg):
     for i in range(1,k):
         print(i)
         coroutine.transfer(neg)
コード例 #17
0
 def negative(k):
     for i in range(1,k):
         print(-i)
         coroutine.transfer(None)