简体   繁体   English

通过带有妖魔化的命名管道读取和写入数据

[英]Reading and writing data via named pipe with demonization

I have two scripts: 我有两个脚本:
The first one ('Reader') is reading data from named pipe and the second ('Writer') is writing data to named pipe. 第一个(“读取器”)正在从命名管道读取数据,第二个(“写入器”)正在将数据写入命名管道。 I am run 'Writer' from daemon (daemon was created with double fork mechanism). 我从守护程序运行“ Writer”(守护程序是使用双叉机制创建的)。 If 'Writer' crash I want to print a message about it in 'Reader'. 如果'Writer'崩溃,我想在'Reader'中打印有关它的消息。 Please see following Python code('Reader'): 请参阅以下Python代码(“阅读器”):

pipe = open(pipe_path, 'r')
while True:
    data = pipe.readline()
    if not data:
        print('Alarm')
        break

But when 'Writer' is crashing then 'Reader' is stuck on following line: 但是,当“ Writer”崩溃时,“ Reader”卡在以下行中:

data = pipe.readline()

But if I run 'Writer' from terminal everything works fine (Alarm message is printing when not data in pipe). 但是,如果我从终端运行“ Writer”,则一切正常(当不在管道中的数据时,正在显示警报消息)。 And everything works fine if I open pipe with: 如果我用以下方法打开管道,则一切正常:

os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK)

But this method not suitable for me because I need to wait a time on start when 'Writer' start write data into pipe 但是这种方法不适合我,因为当“作家”开始将数据写入管道时,我需要等待一段时间才能开始

In order not to create a zombie I do next in daemon: 为了不创建僵尸,我接下来在守护进程中执行以下操作:

def childHandler(signum, frame):
    os.wait3(os.WNOHANG)
signal.signal(signal.SIGCHLD, zombieKiller)

How to prevent the 'Reader' stuck and correctly handle the signal from the child process ? 如何防止“阅读器”卡住并正确处理子进程的信号?

尝试在守护程序中使用此命令:

fdPipe = os.open(pipe_path, os.O_WRONLY | os.O_NONBLOCK)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM