當前位置: 首頁>>代碼示例>>Python>>正文


Python Channel.poison方法代碼示例

本文整理匯總了Python中channel.Channel.poison方法的典型用法代碼示例。如果您正苦於以下問題:Python Channel.poison方法的具體用法?Python Channel.poison怎麽用?Python Channel.poison使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在channel.Channel的用法示例。


在下文中一共展示了Channel.poison方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: P1

# 需要導入模塊: from channel import Channel [as 別名]
# 或者: from channel.Channel import poison [as 別名]
class BufferedChannel:
    """ Channel class.
    Blocking or buffered communication.
    
    >>> from pycsp import *

    >>> @process
    ... def P1(cout):
    ...     while True:
    ...         cout('Hello World')

    >>> C = Channel()
    >>> Spawn(P1(C.writer()))
    
    >>> cin = C.reader()
    >>> cin()
    'Hello World'

    >>> retire(cin)

    Buffered channels are semantically equivalent with a chain
    of forwarding processes.
    >>> B = Channel(buffer=5)
    >>> cout = B.writer()
    >>> for i in range(5):
    ...     cout(i)

    Poison and retire are attached to final element of the buffer.
    >>> poison(cout)

    >>> @process
    ... def sink(cin, L):
    ...     while True:
    ...         L.append(cin())

    >>> L = []
    >>> Parallel(sink(B.reader(), L))
    >>> L
    [0, 1, 2, 3, 4]
    """
    def __init__(self, name=None, buffer=1):
        if name == None:
            # Create unique name
            name = str(random.random())+str(time.time())

        self.name = name
        self.__inChan = Channel(name=name+'inChan')
        self.__outChan = Channel(name=name+'outChan')
        self.__bufferProcess = self.Buffer(self.__inChan.reader(),
                                       self.__outChan.writer(),
                                       N=buffer)

        # Retrieve channel ends
        self.reader = self.__outChan.reader
        self.writer = self.__inChan.writer

        # Set buffer process as deamon to allow kill if mother process
        # exists.
        self.__bufferProcess.daemon = True

        # Start buffer process        
        Spawn(self.__bufferProcess)

    def __pos__(self):
        return self.reader()

    def __neg__(self):
        return self.writer()

    def poison(self):
        self.__inChan.poison()
        self.__outChan.poison()

    @process
    def Buffer(self, cin, cout, N):
        queue = deque()
        poisoned = False
        retired = False
        while True:
            try:
                import pycsp.current
                if pycsp.current.trace:
                    TraceMsg(len(queue))

                # Handling poison / retire
                if (poisoned or retired):
                    if len(queue):
                        try:
                            cout(queue.popleft())
                        except:
                            if poisoned:
                                poison(cin, cout)
                            if retired:
                                retire(cin, cout)
                    else:
                        try:
                            if poisoned:
                                poison(cin, cout)
                            if retired:
                                retire(cin, cout)
#.........這裏部分代碼省略.........
開發者ID:JohannesEbke,項目名稱:a4,代碼行數:103,代碼來源:buffer.py


注:本文中的channel.Channel.poison方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。