2014-01-17 14 views
10

以下簡單腳本會間歇性地掛在subprocess.Popen調用上(大約30%的時間)。
除非use_lock = True,然後它永遠不會掛起,導致我相信子進程不是線程安全的! 預期的行爲是腳本在5-6秒內完成。
爲了演示該錯誤,只需運行「python bugProof.py」幾次,直到它掛起。 Ctrl-C退出。你會看到'後Popen'只出現一次或兩次,但不是第三次。是否是subprocess.Popen不是線程安全的?

#!/bin/sh 
echo "Calculating factorial (anything that's somewhat compute intensive, this script takes 3 sec on my machine" 
ans=1 
counter=0 
fact=999 
while [ $fact -ne $counter ] 
do 
    counter=`expr $counter + 1` 
    ans=`expr $ans \* $counter` 
done 
echo "Factorial calculation done" 
read -p "Test input (this part is critical for bug to occur): " buf 
echo "$buf" 

系統信息: Linux的2.6.32-358.123.2.openstack.el6.x86_64#1 SMP週四09月

import subprocess, threading, fcntl, os, time 
end_time = time.time()+5 
lock = threading.Lock() 
use_lock = False 
path_to_factorial = os.path.join(os.path.dirname(os.path.realpath(__file__)),'factorial.sh') 

def testFunction(): 
    print threading.current_thread().name, '| pre-Popen' 
    if use_lock: lock.acquire() 
    p = subprocess.Popen([path_to_factorial], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
    if use_lock: lock.release() 
    print threading.current_thread().name, '| post-Popen' 
    fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK) 
    fcntl.fcntl(p.stderr, fcntl.F_SETFL, os.O_NONBLOCK) 
    while time.time()<end_time: 
     try: p.stdout.read() 
     except: pass 
     try: p.stderr.read() 
     except: pass 
    print threading.current_thread().name, '| DONE' 

for i in range(3): 
    threading.Thread(target=testFunction).start() 


上述(factorial.sh)引用的外殼腳本26 17:14:58 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux
Python 2.7.3(默認,2013年1月22日,11:34:30)
[GCC 4.4.6 20120305(Red Hat 4.4.6-4) ] on linux2

+0

Putting close_fds = True在Popen調用中也解決了由於某種原因導致的問題。 – Roman

+0

這實際上是我近期在Python中遇到的最令人不快的事情。 Sooo令人沮喪。 –

回答

13

在Python 2.x中,有各種競爭條件影響子進程.Popen。 (例如在2.7上禁用&恢復垃圾收集以防止各種時間問題,但這本身並不是線程安全的)。見例如http://bugs.python.org/issue2320,http://bugs.python.org/issue1336http://bugs.python.org/issue14548針對該領域的一些問題。

在Python 3.2中對子進程進行了大量修改,解決了這些問題(其中包括fork &執行代碼位於C模塊中,而不是在fork和exec之間的關鍵部分中執行一些合理涉及的Python代碼) ,並可在subprocess32模塊中反向移植到最新的Python 2.x版本。請注意PyPI頁面中的以下內容:「在POSIX系統上,它確保在線程應用程序中使用時可靠。」

我可以重現上面的代碼偶爾(約25%)崩潰,但在使用import subprocess32 as subprocess後,我還沒有看到任何失敗100 +運行。

請注意,subprocess32(和Python 3.2 +)默認爲close_fds = True,但是使用subprocess32,即使使用close_fds = False也不會失敗(並非您通常需要這樣做)。