olekb olekb - 9 days ago 5
Python Question

Can I override default read() method in io.BufferedReader?

I need to send only part of the file into STDIN of another process?

#Python 3.5
from subprocess import PIPE, Popen, STDOUT

fh = io.open("test0.dat", "rb")
fh.seek(10000)
p = Popen(command, stdin=fh, stdout=PIPE, stderr=STDOUT)
p.wait()


How do I make sure that
command
will only read 1000 more bytes from stdin and then encounters EOF.
I have no control over how command reads standard input.

Answer

problem is, when passed an handle, Popen tries to get fileno, and uses real OS handles, so it's not possible to fool it easily with other file-like objects.

But you can set Popen stdin as PIPE, write only the correct number of bytes to it, possibly in small chunks, at the rythm you choose, and close it afterwards

import io
from subprocess import PIPE, Popen, STDOUT

fh = io.open("test0.dat", "rb")
fh.seek(10000)


p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
p.stdin.write(fh.read(1000))
# do something & write again
p.stdin.write(fh.read(1000))
# now it's enough: close the input
p.stdin.close()

p.wait()

Be careful though: since stdin and stdout use PIPE, you have to consume stdout to avoid deadlocks.