Blog: 2024-02-13: Difference between revisions

From razwiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 6: Line 6:
print(os.read(proc.stdout.fileno(), 4096))
print(os.read(proc.stdout.fileno(), 4096))
</pre>https://stackoverflow.com/questions/39899074/communicate-multiple-times-with-a-subprocess-in-python
</pre>https://stackoverflow.com/questions/39899074/communicate-multiple-times-with-a-subprocess-in-python

Ok it was about: buffering and flushing

<pre>
import os
import subprocess

proc = subprocess.Popen(['bc'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.stdin.write(b'100+200\n')
proc.stdin.flush()
print(proc.stdout.readline())
</pre>

flush was missing, and readline reads just a line rather than read which blocks

Latest revision as of 10:00, 13 February 2024

How does this work but it doesn't seem to when you use the read and write methods on the io objects?

proc = subprocess.Popen(['bc'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
os.write(proc.stdin.fileno(), b'100+200\n')
print(os.read(proc.stdout.fileno(), 4096))

https://stackoverflow.com/questions/39899074/communicate-multiple-times-with-a-subprocess-in-python

Ok it was about: buffering and flushing

import os
import subprocess

proc = subprocess.Popen(['bc'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.stdin.write(b'100+200\n')
proc.stdin.flush()
print(proc.stdout.readline())

flush was missing, and readline reads just a line rather than read which blocks