2005年07月24日 星期日 21:42
说到pipe,谁能帮我看看下面这个问题。在c.l.py上问的,还没人回答。 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: And Under Windows XP, p.wait() never returns: 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&id=67907&t=1>
2005年07月24日 星期日 22:57
当Collector接收到'exit'退出时,Receiver仍然会向Collector发送'exit'字符串,所 以会Broken pipe。 以下的代码在Windows和Debian上都没有问题。 改动了Receiver # receiver.py (the main program) from subprocess import Popen, PIPE def main(): p = Popen(['python', 'collector.py'], stdout=PIPE, stdin=PIPE) keep_going = False count = 0 for line in p.stdout: data = line.strip() print 'Receiver: ' + line # process(data) count += 1 if count >= 1000: if keep_going == False: print >> p.stdin, 'exit' keep_going = True print 'waiting subprocess exit' p.wait() if __name__ == '__main__': main() #end of receiver.py __ Best Regards, Kebo Wang
2005年07月24日 星期日 23:31
还是有问题,我在你的代码中添加了print,这样wait()就不会block,不知为什么。 附上修改后的Collector: # collector.py import threading import time import sys class Main(object): def __init__(self): self.keep_going = True self.t = threading.Thread(target=self.work) self.t.start() cmd = raw_input() print >> sys.stderr, 'Collector: ' + cmd while cmd != 'exit': cmd = raw_input() print >> sys.stderr, 'Collector: ' + cmd self.keep_going = False self.t.join() def work(self): while self.keep_going: print '$' * 39 print >> sys.stderr, 'Collector: ' + '$' * 39 if __name__ == '__main__': Main() #end of collector.py __ Best Regards, Kebo Wang >-----Original Message----- >From: python-chinese-bounces at lists.python.cn >[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of Wang Kebo >Sent: Sunday, July 24, 2005 10:58 PM >To: python-chinese at lists.python.cn >Subject: RE: [python-chinese] what's wrong with my code using >subprocess? > >当Collector接收到'exit'退出时,Receiver仍然会向Collector发送'exit'字符串, 所 >以会Broken pipe。 > >以下的代码在Windows和Debian上都没有问题。 >改动了Receiver > ># receiver.py (the main program) >from subprocess import Popen, PIPE > >def main(): > p = Popen(['python', 'collector.py'], stdout=PIPE, stdin=PIPE) > keep_going = False > count = 0 > for line in p.stdout: > data = line.strip() > print 'Receiver: ' + line > # process(data) > count += 1 > if count >= 1000: > if keep_going == False: > print >> p.stdin, 'exit' > keep_going = True > print 'waiting subprocess exit' > p.wait() > >if __name__ == '__main__': > main() >#end of receiver.py > >__ >Best Regards, > >Kebo Wang > > >_______________________________________________ >python-chinese list >python-chinese at lists.python.cn >http://python.cn/mailman/listinfo/python-chinese >
Zeuux © 2025
京ICP备05028076号