简体   繁体   中英

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). If 'Writer' crash I want to print a message about it in 'Reader'. Please see following Python code('Reader'):

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:

data = pipe.readline()

But if I run 'Writer' from terminal everything works fine (Alarm message is printing when not data in pipe). 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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