what's wrong with my code using subprocess?

Q

Qiangning Hong

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>
 
T

Tim Lesher

I see the same behavior as you do. On Windows, the wait() isn't
hanging--what's happening is that the subprocess just never receives
anything.

I don't quite understand why, but it works fine when I change the "if"
clause in receiver.py to this:

if count >= 1000:
p.communicate('exit')
p.wait()
break

Note the final break: if this isn't here, the next for iteration raises
an exception, as p.stdout has been closed by p.communicate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top