.communicate() writes input (there is no input in this case so it just closes subprocess' stdin to indicate to the subprocess that there is no more input), reads all output, and waits for the subprocess to exit.

The exception EOFError is raised in the child process by raw_input() (it expected data but got EOF (no data)).

p.stdout.read() hangs forever because it tries to read all output from the child at the same time as the child waits for input (raw_input()) that causes a deadlock.

To avoid the deadlock you need to read/write asynchronously (e.g., by using threads or select) or to know exactly when and how much to read/write, for example:

from subprocess import PIPE, Popen

p = Popen(["python", "-u", "1st.py"], stdin=PIPE, stdout=PIPE, bufsize=1)
print p.stdout.readline(), # read the first line
for i in range(10): # repeat several times to show that it works
    print >>p.stdin, i # write input
    p.stdin.flush() # not necessary in this case
    print p.stdout.readline(), # read output

print p.communicate("n\n")[0], # signal the child to exit,
                               # read the rest of the output, 
                               # wait for the child to exit

Note: it is a very fragile code if read/write are not in sync; it deadlocks.

Beware of block-buffering issue (here it is solved by using "-u" flag that turns off buffering for stdin, stdout in the child).

bufsize=1 makes the pipes line-buffered on the parent side.

Answer from jfs on Stack Overflow
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
1 week ago - The input argument is passed to Popen.communicate() and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string if encoding or errors is specified or text is true.
Top answer
1 of 3
80

.communicate() writes input (there is no input in this case so it just closes subprocess' stdin to indicate to the subprocess that there is no more input), reads all output, and waits for the subprocess to exit.

The exception EOFError is raised in the child process by raw_input() (it expected data but got EOF (no data)).

p.stdout.read() hangs forever because it tries to read all output from the child at the same time as the child waits for input (raw_input()) that causes a deadlock.

To avoid the deadlock you need to read/write asynchronously (e.g., by using threads or select) or to know exactly when and how much to read/write, for example:

from subprocess import PIPE, Popen

p = Popen(["python", "-u", "1st.py"], stdin=PIPE, stdout=PIPE, bufsize=1)
print p.stdout.readline(), # read the first line
for i in range(10): # repeat several times to show that it works
    print >>p.stdin, i # write input
    p.stdin.flush() # not necessary in this case
    print p.stdout.readline(), # read output

print p.communicate("n\n")[0], # signal the child to exit,
                               # read the rest of the output, 
                               # wait for the child to exit

Note: it is a very fragile code if read/write are not in sync; it deadlocks.

Beware of block-buffering issue (here it is solved by using "-u" flag that turns off buffering for stdin, stdout in the child).

bufsize=1 makes the pipes line-buffered on the parent side.

2 of 3
29

Do not use communicate(input=""). It writes input to the process, closes its stdin and then reads all output.

Do it like this:

p=subprocess.Popen(["python","1st.py"],stdin=PIPE,stdout=PIPE)

# get output from process "Something to print"
one_line_output = p.stdout.readline()

# write 'a line\n' to the process
p.stdin.write('a line\n')

# get output from process "not time to break"
one_line_output = p.stdout.readline() 

# write "n\n" to that process for if r=='n':
p.stdin.write('n\n') 

# read the last output from the process  "Exiting"
one_line_output = p.stdout.readline()

What you would do to remove the error:

all_the_process_will_tell_you = p.communicate('all you will ever say to this process\nn\n')[0]

But since communicate closes the stdout and stdin and stderr, you can not read or write after you called communicate.

🌐
Python Module of the Week
pymotw.com › 2 › subprocess
subprocess – Work with additional processes - Python Module of the Week
In the second example, the same 10 numbers are written but the output is read all at once using communicate(). import subprocess print 'One line at a time:' proc = subprocess.Popen('python repeater.py', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) for i in range(10): proc.stdin.write('%d\n' % i) output = proc.stdout.readline() print output.rstrip() remainder = proc.communicate()[0] print remainder print print 'All output at once:' proc = subprocess.Popen('python repeater.py', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, ) for i in range(10): proc.stdin.write('%d\n' % i) output = proc.communicate()[0] print output
🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - In this example, the Popen class is used to create two child processes, one for the ls command and one for the grep command. The stdout of the ls command is connected to the stdin of the grep command using subprocess.PIPE, which creates a pipe between the two processes. The communicate() method is used to send the output of the ls command to the grep command and retrieve the filtered output...
🌐
Real Python
realpython.com › python-subprocess
The subprocess Module: Wrapping Programs With Python – Real Python
January 18, 2025 - These are the standard streams—a cross-platform pattern for process communication. Sometimes the child process inherits these streams from the parent. This is what’s happening when you use subprocess.run() in the REPL and are able to see the output of the command. The stdout of the Python interpreter is inherited by the subprocess.
🌐
GitHub
gist.github.com › waylan › 2353749
Writing to a python subprocess pipe · GitHub
Unfortunately, we have to create ... We can do better: from subprocess import Popen, PIPE p = Popen('less', stdin=PIPE) for x in xrange(100): p.stdin.write('Line number %d.\n' % x) p.stdin.close() p.wait()...
Find elsewhere
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 261056 › communicate-using-subprocess
python - Communicate using subprocess [SOLVED] | DaniWeb
February 17, 2010 - @txwooley: subprocess creates a child process that stays "alive" until that child exits. Popen.communicate() both sends any input you give it and then waits for the process to finish, so it is fine for one-shot inputs (like answering a single yes/no) but it closes the stdin it writes to before returning.
🌐
Python 101
python101.pythonlibrary.org › chapter19_subprocess.html
Chapter 19 - The subprocess Module — Python 101 1.0 documentation
Or you could just put all this code into a saved Python file and run that. Note that using the wait method can cause the child process to deadlock when using the stdout/stderr=PIPE commands when the process generates enough output to block the pipe. You can use the communicate method to alleviate this situation. We’ll be looking at that method in the next section. Now let’s try running Popen using multiple arguments: >>> subprocess.Popen(["ls", "-l"]) <subprocess.Popen object at 0xb7451001>
🌐
Medium
medium.com › more-python › concurrency-in-python-part-vii-the-subprocess-module-bb54e4134eaa
Concurrency in Python, Part VII — The subprocess Module | by Mikhail Berkov | More Python | Medium
February 9, 2025 - For example, here is how you can pipe the output of ps -aux into grep python: import subprocess ps = subprocess.Popen(["ps", "-aux"], stdout=subprocess.PIPE, text=True) grep = subprocess.Popen(["grep", "python"], stdin=ps.stdout, stdout=sub...
🌐
Masterccc
masterccc.github.io › memo › process_com
Python subprocess communication · Master 0xCcC
October 4, 2019 - Cheatsheet - Python subprocess communication Link to heading Imports Link to heading from subprocess import Popen, PIPE Imports for signals Link to heading import signal, os Start process Link to heading process = Popen(['./sum 50'], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) Check if ...
🌐
Python
docs.python.org › 3 › library › asyncio-subprocess.html
Subprocesses — Python 3.14.4 documentation
February 23, 2026 - Both create_subprocess_exec() and create_subprocess_shell() functions return instances of the Process class. Process is a high-level wrapper that allows communicating with subprocesses and watching for their completion.
🌐
Simplilearn
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
December 15, 2025 - Learn about Python's subprocess module for executing external commands. Discover how to manage processes and handle inputs/outputs efficiently.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › subprocess.Popen.communicate.html
subprocess.Popen.communicate — Python Standard Library
subprocess.Popen.communicate · View page source · Popen.communicate(input=None)[source]¶ · Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent ...
🌐
Ansible
forum.ansible.com › archives › ansible developer
Usage of python's subprocess.Popen.communicate() - Ansible Developer - Ansible
January 15, 2014 - Hi, I wasn’t sure whether to report this as a bug or not. The issue was this: I have an rsync task that I use for efficiently copying directory hierarchies to remote machines. The symptom is that it would sometimes hang during the rsync task. ps would reveal that the rsync process was done, but being held onto by the parent process (defunct).
🌐
Google Groups
groups.google.com › g › ansible-devel › c › yrQ_UaGOv0g
Usage of python's subprocess.Popen.communicate()
This new process outlives the rsync/ssh that spawned it, and holds onto stderr. Ansible is using Popen.communicate() from python's subprocess module, which not only blocks until the process finishes (that is, until wait() returns) but also blocks until stdout and stderr from the command are closed.
🌐
Eli Bendersky
eli.thegreenplace.net › 2017 › interacting-with-a-long-running-child-process-in-python
Interacting with a long-running child process in Python - Eli Bendersky's website
July 11, 2017 - communicate has a very convenient timeout argument starting with Python 3.3 [1], letting us know if the child does not exit for some reason. A more sophisticated technique could be to send the child a SIGKILL (with proc.kill) if it didn't exit due to SIGTERM. ... $ python3.6 interact-http-server.py == subprocess exited with rc = -15 Serving HTTP on 0.0.0.0 port 8070 (http://0.0.0.0:8070/) ...