本文整理汇总了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)
#.........这里部分代码省略.........