Python论坛  - 讨论区

标题:[python-chinese] what's wrong with my code using subprocess?

2005年07月24日 星期日 21:42

Qiangning Hong hongqn at gmail.com
Sun Jul 24 21:42:40 HKT 2005

说到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:

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


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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月24日 星期日 22:57

Wang Kebo mep_ at 163.com
Sun Jul 24 22:57:57 HKT 2005

当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



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月24日 星期日 23:31

Wang Kebo mep_ at 163.com
Sun Jul 24 23:31:13 HKT 2005

还是有问题,我在你的代码中添加了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
>



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号