subprocess.Popen.communicate()不能正常執行

在本身電腦上是好的,可是移植到windows2003的服務器上運行的時候每次就會卡着這行代碼這裏不能正常執行。python

搜尋資料中發現有以下解釋(參考https://bugs.python.org/issue1731717):windows

It appears that subprocess calls a module global "_cleanup()" whenever opening a new subprocess. This method is meant to reap any child processes that have terminated and have not explicitly cleaned up. These are processes you would expect to be cleaned up by GC, however, subprocess keeps a list of of all spawned subprocesses in _active until they are reaped explicitly so it can cleanup any that nolonger referenced anywhere else.

The problem is lots of methods, including poll() and wait(), check self.returncode and then modify it. Any non-atomic read/modify action is inherently non-threadsafe. And _cleanup() calls poll() on all un-reaped child processes. If two threads happen to try and spawn subprocesses at once, these _cleanup() calls collide..

The way to fix this depends on how thread-safe you want to make it. If you want to share popen objects between threads to wait()/poll() with impunity from any thread, you should add a recursive lock attribute to the Popen instance and have it lock/release it at the start/end of every method call.

其餘相關資料:服務器

https://bugs.python.org/issue9127app

https://hg.python.org/cpython/rev/767420808a62ide

----------------------12月10日更新分割線---------------------------this

結果:就是程序的問題,或者更明確地說是child子程序的問題。因此說,程序的世界裏是沒有未解之謎的,一切都是有據可循的,若是出現了奇怪的錯誤,只須要默默好好的檢查本身代碼。atom

相關文章
相關標籤/搜索