This is a follow-up to this question which I closed yesterday in haste
I have two Python processes piped together with the 2nd one reading from stdin. When the feeder process (which writes its output to stdout) stops (e.g. is killed), I expected the code below to generate an exception, as was suggested by others:
while True:
try:
l = sys.stdin.readline()
## process l
except Exception, e:
## handle exceptions
except IOError, e:
## handle IO exceptions
if e.errno == errno.EPIPE:
## handle EPIPE exceptions
However, that does not happen. Instead, sys.stdin.readline()
simply returns an empty l
.
So 2 questions:
- Is it possible to modify this code to get an exception when the feeder process dies?
- Can i somehow find the process ID of the feeder process inside the 2nd process? In other words, if my pipe is
./step1.py | ./step2.py
I want to find the process ID of step1 inside step2. I triedos.getppid()
but that returns the id of the bach process that runs step2, not step1.
Thanks!