what's wrong with my code using subprocess?

Qiangning Hong hongqn at gmail.com
Sun Jul 24 04:50:37 CEST 2005

I decide to seperate my data collection routine from my data analysis
and storage program to a seperate process, so I try to use the new
subprocess model in Python 2.4.

The main program spawns the subprocess and receives data from the
pipe.  When some event occurs (e.g. the user clicks the 'Stop' button
on GUI), the main program will send the subprocess a command to change
its behavior or ask it to exit.

However, my code (attached below) doesn't work.  Under Linux, the output is:
waiting subprocess exit
Traceback (most recent call last):
  File "receiver.py", line 19, in ?
  File "receiver.py", line 13, in main
    print >>p.stdin, 'exit'
IOError: [Errno 32] Broken pipe

And Under Windows XP, p.wait() never returns:
waiting subprocess exit
[hanging here]

What's wrong?  

# collector.py
import threading

class Main(object):
    def __init__(self):
        self.keep_going = True
        self.t = threading.Thread(target=self.work)

        cmd = raw_input()
        while cmd != 'exit':
            cmd = raw_input()

        self.keep_going = False

    def work(self):
        while self.keep_going:
            print '$' * 82

if __name__ == '__main__':

# receiver.py (the main program)
from subprocess import Popen, PIPE

def main():
    p = Popen(['python', 'collector.py'], stdout=PIPE, stdin=PIPE)
    count = 0
    for line in p.stdout:
        data = line.strip()
        # process(data)
        count += 1
        if count >= 1000:
            print >>p.stdin, 'exit'
            print 'waiting subprocess exit'

if __name__ == '__main__':

Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
   -- Sybren Stuvel @ c.l.python

Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>

More information about the Python-list mailing list