I tried to understand FIFO using Python under linux, and I found strange behavior that I do not understand.
Below is fifoserver.py
import sys import time def readline(f): s = f.readline() while s == "": time.sleep(0.0001) s = f.readline() return s while True: f = open(sys.argv[1], "r") x = float(readline(f)) g = open(sys.argv[2], "w") g.write(str(x**2) + "\n") g.close() f.close() sys.stdout.write("Processed " + repr(x) + "\n")
and this is fifoclient.py
import sys import time def readline(f): s = f.readline() while s == "": time.sleep(0.0001) s = f.readline() return s def req(x): f = open("input", "w") f.write(str(x) + "\n") f.flush() g = open("output", "r") result = float(readline(g)) g.close() f.close() return result for i in range(100000): sys.stdout.write("%i, %s\n" % (i, i*i == req(i)))
I also created two FIFOs using mkfifo input and mkfifo output .
I don’t understand why, when I start the server (with python fifoserver.py input output ) and the client (with python fifoclient.py ) from two consoles after some requests, the client crashes with a “broken pipe” error to f.flush() . Please note that before the crash, I saw from several hundred to several thousand correctly processed requests that work fine.
What is the problem in my code?
python linux broken-pipe fifo mkfifo
6502
source share