python subprocess pipe unbuffered behaviour
I've the below piece of code to read data from a child process as its generated and write to a file.
from subprocess import Popen, PIPE
proc = Popen('..some_shell_command..', shell=True, stdout=PIPE)
fd = open("/tmp/procout", "wb")
while True:
data = proc.stdout.read(1024)
if len(data) == 0:
break
fd.write(data)
fd.close()
'Popen' default bufsize is 0 => unbuffered. What will happen if for some reason the write-to-file operation experiences a huge latency?
Answering to your questions:
write operation after exceeding pipe-max-size limit (cat /proc/sys/fs/pipe-max-size); write syscall will be called. In case of non-blocking IO I hope write syscall will return EAGAIN or other system-specific error. So actually the application will stuck while calling write system call waiting for the pipe buffer will available. It doesn't mean that it will hang. For example if an application implements some kind of internal queue and it have more than one thread, it can continue to work and add any data to it's queue while the writting-out thread will wait for the buffer.
下一篇: python子进程管道无缓冲行为
