【发布时间】:2017-09-12 14:42:33
【问题描述】:
我很想为我的一个项目找出一些东西,但在一个问题上挂了:
我正在使用 FIFO 操作将“信号”(简单 T/F)从一个模块发送到另一个模块。一个模块打开 FIFO 进行写入,另一个模块打开 FIFO 进行读取。这里的目标是让读取模块在写入模块收到命令后立即读取和显示。写入模块打开了FIFO,但是读取模块好像没有这样做。
我正在尝试做的事情是否可能?我试图在 _threads 中旋转这两个操作,以保持每个模块中的多个进程。请注意,这两个模块都在我为简洁起见未包括在内的类中(解释“自我”)。
原发送模块
def pipe_relay(self):
FIFO = 'pipe_relay'
thread_num = num
try:
os.mkfifo(FIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
while self.relay_switch:
print("Opening FIFO...")
with open(FIFO) as fifo:
print("FIFO opened")
while self.relay_switch:
data = fifo.write(signal)
if len(data) == 0:
print("Writer is closed")
break
print('Write: "{0}"'.format(data))
更新发送模块
我意识到我不想将我扔给它的数据连续写入 FIFO,所以我删除了 while() 语句。现在看来,FIFO 似乎根本不会打开……
def pipe_relay(self, num, signal):
FIFO = 'pipe_relay'
thread_num = num
try:
os.mkfifo(FIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
print("Opening FIFO...")
# does not proceed past this point
with open(FIFO, mode = 'w') as fifo:
print("FIFO opened")
data = fifo.write(signal)
if len(data) == 0:
print("Writer is closed")
print('Write: "{0}"'.format(data))
fifo.close()
接收模块
def pipe_receive(self):
FIFO = 'pipe_relay'
try:
os.mkfifo(FIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
# module proceeds to here, but no further
with open(FIFO) as fifo:
print("FIFO opened (receiver)")
while True:
data = fifo.read()
if len(data) == 0:
print("Writer is closed")
break
print('Read signal: "{0}"'.format(data))
self.DISPLAY['text'] = data
print("this is in 'pipe_receieve'")
编辑
运行 Ubuntu 17.04。该项目是为 Python 3.5 解释器编写的。
【问题讨论】:
-
操作系统、平台信息?
-
我会发布编辑
-
当您运行发送模块时,会显示哪些打印语句?你确定你正在给先进先出写信吗?
-
我很确定你应该在写完之后
fifo.flush()。见stackoverflow.com/questions/7048095/…
标签: python python-3.x ipc fifo mkfifo