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:
<output>
waiting subprocess exit
Traceback (most recent call last):
  File "receiver.py", line 19, in ?
    main()
  File "receiver.py", line 13, in main
    print >>p.stdin, 'exit'
IOError: [Errno 32] Broken pipe
</output>

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

What's wrong?  

# collector.py
import threading

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

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

        self.keep_going = False
        self.t.join()

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

if __name__ == '__main__':
    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'
            p.wait()

if __name__ == '__main__':
    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&amp;id=67907&amp;t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to