Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 26 characters in body
Source Link
Ivan Baldin
  • 3.5k
  • 3
  • 24
  • 14

I'm having problems redirecting stdio of another program using subprocess module. Just reading from stdout results in hanging, and Popen.communicate() works but it closes pipes after reading/writing. What's the easiest way to implement this?

I was playing around with this on windows:

import subprocess proc = subprocess.Popen('python -c "while True: print \'Hi %s!\' % raw_input()"', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: proc.stdin.write('world\n') proc_read = proc.stdout.readlinesreadline()  for line in proc_read:  if lineproc_read:   print lineproc_read

I'm having problems redirecting stdio of another program using subprocess module. Just reading from stdout results in hanging, and Popen.communicate() works but it closes pipes after reading/writing. What's the easiest way to implement this?

I was playing around with this on windows:

import subprocess proc = subprocess.Popen('python -c "while True: print \'Hi %s!\' % raw_input()"', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: proc.stdin.write('world\n') proc_read = proc.stdout.readlines()  for line in proc_read:  if line:   print line

I'm having problems redirecting stdio of another program using subprocess module. Just reading from stdout results in hanging, and Popen.communicate() works but it closes pipes after reading/writing. What's the easiest way to implement this?

I was playing around with this on windows:

import subprocess proc = subprocess.Popen('python -c "while True: print \'Hi %s!\' % raw_input()"', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: proc.stdin.write('world\n') proc_read = proc.stdout.readline() if proc_read: print proc_read
Source Link
Ivan Baldin
  • 3.5k
  • 3
  • 24
  • 14

How to properly interact with a process using subprocess module

I'm having problems redirecting stdio of another program using subprocess module. Just reading from stdout results in hanging, and Popen.communicate() works but it closes pipes after reading/writing. What's the easiest way to implement this?

I was playing around with this on windows:

import subprocess proc = subprocess.Popen('python -c "while True: print \'Hi %s!\' % raw_input()"', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: proc.stdin.write('world\n') proc_read = proc.stdout.readlines() for line in proc_read: if line: print line