470,596 Members | 1,594 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

what's wrong with my code using subprocess?

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>
Jul 24 '05 #1
1 1985
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.

Jul 25 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dr. Who | last post: by
4 posts views Thread by Marc Carter | last post: by
14 posts views Thread by Ernesto | last post: by
1 post views Thread by Jim | last post: by
5 posts views Thread by Grant Edwards | last post: by
9 posts views Thread by Phoe6 | last post: by
reply views Thread by Gabriel Genellina | last post: by
1 post views Thread by Mark Shewfelt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.