IOError: [Errno 32] Broken pipe

Python version I use is 2.7.3 in Debian Wheezy.

The subprocess example code from here creates annoying error:

Traceback (most recent call last):
File "process_example4.py", line 8, in
sys.stdout.flush()
IOError: [Errno 32] Broken pipe

process_example4.py

import sys

while True:
input = sys.stdin.readline()
# Prevent IOError
if not input:
break

sys.stdout.write('Received: %s'%input)
sys.stdout.flush()

run_example4.py

import subprocess
import time

process = subprocess.Popen(['python', 'process_example4.py'], shell=False,\
stdin=subprocess.PIPE, stdout=subprocess.PIPE)

for i in range(5):
process.stdin.write('%d\n' % i)
output = process.stdout.readline()
print output
time.sleep(1)

‘python run_example4.py’ supposedly produces the following output by the author. Because the author does not mention run-time error at all.
Received: 0

Received: 1

Received: 2

Received: 3

Received: 4

Well, the actual output of the example code creates IOError at the end of running. In interactive mode the error appears after exit of the interpreter.

$ python run_example4.py

Received: 0

Received: 1

Received: 2

Received: 3

Received: 4

Traceback (most recent call last):
File "process_example4.py", line 10, in
sys.stdout.flush()
IOError: [Errno 32] Broken pipe

This codes won’t solve IOError at least in my case.

from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)

The author used Python 2.5x to produce above codes. I’m certain it had to run perfectly. The most frustrating experience of learning programming language is whenever hits the unexpected error from basic sample codes like this.

Francis’ solution works too. I made two python script files: process_example5.py and run_example5.py


# process_example5.py

import sys
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)

while True:
input = sys.stdin.readline()
sys.stdout.write('Received: %s'%input)
sys.stdout.flush()


# run process_example5.py
# 11/01/2012

import subprocess
import time

process = subprocess.Popen(['python', 'process_example5.py'], shell=False,\
stdin=subprocess.PIPE, stdout=subprocess.PIPE)

for i in range(5):
process.stdin.write('%d\n' % i)
output = process.stdout.readline()
print output
time.sleep(1)

About janpenguin

Email: k2.mountain [at] gmail [dot] com Every content on the blog is made by Free and Open Source Software in GNU/Linux.
This entry was posted in GNU/Linux, Python and tagged , , , , . Bookmark the permalink.

2 Responses to IOError: [Errno 32] Broken pipe

  1. Francis says:

    wahhah cannot imagine we’re looking at the same thing.

    Try this for test3.py, it works for me:

    #!/usr/bin/env python2.5
    import sys
    from signal import signal, SIGPIPE, SIG_DFL
    signal(SIGPIPE, SIG_DFL)

    while True:
    input = sys.stdin.readline()
    sys.stdout.write(‘Received: %s’%input)
    sys.stdout.flush()

  2. kostarchild says:

    Hi Francis,

    Your solution works too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s