As oyu found out yourself, .communicate()
handles this.
The reason for your observations are the following:
- Two pipes connect your program and the subprocess, namely its stdin and its stdout.
- Depending on the OS, each of these pipes has a buffer size of 65536 bytes.
- Besides, depending on the program and what it does, it reads 65536 bytes as well and writes some data out.
So, if you write 196608 bytes, the first 65536 bytes are sent to stdin, read by the program and (if it is cat
, for example) output to the stdout pipe. The second 65536 bytes are put to stdin, read by the program and tried to write to stdout, but there it blocks as stdout is full. The third 65536 bytes are written to stdin. For each excess byte, writing blocks because stdin is full as well.
The solution is as you write: let .communicate()
handle the whole thing. It is prepared for this situation and handles it, depending on the OS either with threading, poll or select calls.