本文整理匯總了Python中channel.Channel.reader方法的典型用法代碼示例。如果您正苦於以下問題:Python Channel.reader方法的具體用法?Python Channel.reader怎麽用?Python Channel.reader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類channel.Channel
的用法示例。
在下文中一共展示了Channel.reader方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: P1
# 需要導入模塊: from channel import Channel [as 別名]
# 或者: from channel.Channel import reader [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)
#.........這裏部分代碼省略.........