I’m starting the script with ./file.py < pipe >> logfile
and the script is:
while True:
try:
I = raw_input().strip().split()
except EOFError:
continue
doSomething()
How could I better handle named pipe? This script always run at 100% CPU and it need to be real-time so I cannot use time.sleep.
At EOF you will loop forever getting yet another EOF. No more input will be done after EOF.
EOF does not mean a “gap” in the data. It means the named socket was disconnected and can no longer be used.
If you want “real-time” data, you have to read individual bytes from the socket until you get a complete “message”. Perhaps a message ends with a
'\n'. You can’t useraw_input.You have to use
sys.stdin.read(1)to get bytes.Pipes, BTW, are buffered. So you won’t get real-time anything. If you want “real-time” you have to use UDP sockets, not TCP pipes.