本文介绍了subprocess - 破坏管道错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有人可以解释一下管子坏了吗?以下产生了一个 损坏的管道错误: ---------- 导入子流程为子 p = sub.Popen([" ls"," -al"," ../& quot;],stdin = sub.PIPE,stdout = sub.PIPE) 打印p.stdout.read() #outputs文件正确 p.stdin.write (ls \ n) #IOError:[Errno 32]破管 -----------Hi,Can someone explain what a broken pipe is? The following produces abroken pipe error:----------import subprocess as subp = sub.Popen(["ls", "-al", "../"], stdin=sub.PIPE, stdout=sub.PIPE)print p.stdout.read()#outputs the files correctlyp.stdin.write("ls\n")#IOError: [Errno 32] Broken pipe-----------推荐答案 7月2日下午1点12分,7stud< bbxx789_0 ... @ yahoo.comwrote:On Jul 2, 1:12 pm, 7stud <bbxx789_0...@yahoo.comwrote: 有人可以解释一下管子坏了吗?以下产生了一个 损坏的管道错误: ---------- 导入子流程为子 p = sub.Popen([" ls"," -al"," ../& quot;],stdin = sub.PIPE,stdout = sub.PIPE) 打印p.stdout.read() #outputs文件正确 p.stdin.write (ls \ n) #IOError:[Errno 32]破管 -----------Hi,Can someone explain what a broken pipe is? The following produces abroken pipe error:----------import subprocess as subp = sub.Popen(["ls", "-al", "../"], stdin=sub.PIPE, stdout=sub.PIPE)print p.stdout.read()#outputs the files correctlyp.stdin.write("ls\n")#IOError: [Errno 32] Broken pipe----------- 您看到此错误,因为sub.Popen在子进程终止后关闭stdin和 stdout(它必须为 $ b完成) $ b p.stdout.read()返回一个结果)。 因此你试图写一个读者已经关闭的管道已经关闭了它,因此错误信息。 问候 SteveYou are seeing this error because sub.Popen closes both stdin andstdout once the subprocess terminates (which it must have done forp.stdout.read() to return a result).Consequently you are trying to write to a pipeline whose reader hasalready closed it, hence the error message.regardsSteve 7月2日,11:32 am,holden ... @ gmail.com < holden ... @ gmail.comwrote:On Jul 2, 11:32 am, "holden...@gmail.com" <holden...@gmail.comwrote: 7月2日下午1点12分,7stud< bbxx789_0 ... @ yahoo.comwrote:On Jul 2, 1:12 pm, 7stud <bbxx789_0...@yahoo.comwrote: 您好, Hi, 有人可以解释一下管道坏了吗?以下产生了一个 损坏的管道错误: Can someone explain what a broken pipe is? The following produces a broken pipe error: ---------- import subprocess as sub ---------- import subprocess as sub p = sub.Popen([" ls"," -al"," ../ "],stdin = sub.PIPE,stdout = sub.PIPE) p = sub.Popen(["ls", "-al", "../"], stdin=sub.PIPE, stdout=sub.PIPE) print p.stdout.read() #outputs文件正确 print p.stdout.read() #outputs the files correctly p.stdin.write(" ls \ n") # IOError:[Errno 32]管道坏了 ----------- p.stdin.write("ls\n") #IOError: [Errno 32] Broken pipe ----------- 你看到这个错误因为sub .Popen在子进程终止后关闭stdin和 stdout(它必须为 p.stdout.read()返回结果)。 因此,您正在尝试写入一个管道,其读取器已经关闭了它,因此出现了错误消息。 问候 SteveYou are seeing this error because sub.Popen closes both stdin andstdout once the subprocess terminates (which it must have done forp.stdout.read() to return a result).Consequently you are trying to write to a pipeline whose reader hasalready closed it, hence the error message.regards Steve 感谢您的回复。那么你是说只有这样你才能从子管道中获取数据吗?Hi,Thanks for the response. So are you saying that the only way you canget data out of a pipe is when the subprocess has terminated?为什么下面的程序没有写入文件? driver.py ------- 导入subprocess as sub p = sub.Popen([" python"," -u"," test1.py"],stdin = sub.PIPE, stdout = sub.PIPE) p.stdin.write(" text3") 而True: 通票 ------- test1.py: ------ --- 导入系统 data = sys.stdin.read() f = open(" ; aaa.txt"," w") f.write(data +" \ n") f.close() ----------- 按Ctrl + C结束程序并查看文件后,文本 wasn'写入文件。但是,如果我将driver.py更改为 以下它可以工作: driver.py: ---- ------ 导入subprocess as sub p = sub.Popen([" python"," -u"," test1 .py"],stdin = sub.PIPE, stdout = sub.PIPE) p.stdin.write(" text3") ------- 好​​的。所以看起来数据被捕获在缓冲区中 - 即使 管道默认情况下应该是无缓冲的。但是这不起作用: driver.py ---------- import subprocess as sub p = sub.Popen([" python"," -u"," test1.py"],stdin = sub.PIPE, stdout = sub.PIPE) p.stdin.write(" text4") p.stdin.flush() 而真实: 通过 ------- 它只是挂起,然后当我按下Ctrl + C并查看文件时, 数据不在那里。Why doesn''t the following program write to the file?driver.py-------import subprocess as subp = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,stdout=sub.PIPE)p.stdin.write("text3")while True:pass-------test1.py:---------import sysdata = sys.stdin.read()f = open("aaa.txt", "w")f.write(data + "\n")f.close()-----------After I hit Ctrl+C to end the program and look in the file, the textwasn''t written to the file. But, if I change driver.py to thefollowing it works:driver.py:----------import subprocess as subp = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,stdout=sub.PIPE)p.stdin.write("text3")-------Ok. So that looks like the data is caught in a buffer--even though thepipes should be unbuffered by default. But this doesn''t work:driver.py----------import subprocess as subp = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,stdout=sub.PIPE)p.stdin.write("text4")p.stdin.flush()while True:pass-------It just hangs, and then when I hit Ctrl+C and look in the file, thedata isn''t in there. 这篇关于subprocess - 破坏管道错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-18 06:24
查看更多