2016-09-13 98 views
4
  child = subprocess.Popen(command, 
         shell=True, 
         env=environment, 
         close_fds=True, 
         stdout=subprocess.PIPE, 
         stderr=subprocess.STDOUT, 
         bufsize=1, 
            ) 

      subout = "" 
      with child.stdout: 
       for line in iter(child.stdout.readline, b''): 
        subout += line 
      logging.info(subout) 
      rc = child.wait() 

有時(間歇地),這永遠掛起。 不知道它掛在iter(child.stdout.readline)child.wait()蟒蛇子過程.Popen掛

ps -ef它Popens過程,這一過程不再存在

我的猜測是,它已經做BUFSIZE使child.stdout.readline是怎麼回事永遠,但我不知道如何測試,併爲這種間斷性地發生

我可以實現報警但林不知道如果我真的不能告訴popen'd過程是否只是緩慢或掛

是適當的讓我們一起ssume,要麼child.stdout.readline或等待()永遠掛起,除了警報我可以採取什麼行動?

回答

5

你很可能會打這explained in the documentation僵局:

Popen.wait()

等待子進程終止。設置並返回returncode屬性。

警告:使用stdout=PIPE和/或stderr=PIPE時這將死鎖和子進程產生足夠的輸出到管道,使得它阻止等待OS管緩衝器接受更多數據。使用communicate()來避免這種情況。

解決方法是使用Popen.communicate()

+0

謝謝你使用溝通! – ealeon