2011-07-28 109 views
1

Erm ...我保證我徹底搜索了網絡,找不到滿意的答案。 <Python看門狗應用程序

我想爲我的python應用程序做一個'控制器',我的意思是,它應該被執行,然後產生服務器,等待提取出口信號,如果有必要,重新啓動腳本。

這段代碼很雜亂,但這就是我迄今爲止所做的...它始終以無法識別的exitSignal 1返回,甚至不會產生服務器進程!有人可以給我點光嗎?

#Server controller version 0.1 
import os 
import time 
from datetime import datetime 

Log = file("C:/Users/Admin/Desktop/Python/Server/WIP.log", 'w') 

def runServer(): 
    exitSignal = os.spawnv(os.P_WAIT, 'C:/Python2.7/python.exe', ['python.exe',   'C:/Users/Admin/Desktop/Python/Server/WIP.py']) 
    print str(datetime.today())+" - Server started" 
    Log.write("\n"+str(datetime.today())+" - Server started") 

    if exitSignal == "0": 
     print str(datetime.today())+" - Server exited succesfully." 
     Log.write("\n"+str(datetime.today())+" - Server exited succesfully.") 

    elif exitSignal == "10": 
     print str(datetime.today())+" - Rebooting server immediately." 
     Log.write("\n"+str(datetime.today())+" - Rebooting server immediately.") 
     runServer() 

    elif exitSignal == "11": 
     print str(datetime.today())+" - Rebooting server in 5 minutes." 
     Log.write("\n"+str(datetime.today())+" - Rebooting server in 5 minutes.") 
     time.sleep(300) 
     runServer() 
     print str(datetime.today())+" - Server rebooted." 
     Log.write("\n"+str(datetime.today())+" - Server rebooted.") 

    else: 
     print str(datetime.today())+" - Unrecognized exitSignal code: %s" % str(exitSignal) 
     Log.write("\n"+str(datetime.today())+" - Unrecognized exitSignal code: %s" % str(exitSignal)) 

if __name__ == "__main__": 
    print str(datetime.today())+" - Controller started." 
    Log.write("\n"+str(datetime.today())+" - Controller started") 
    runServer() 

對不起,亂碼! (= ^,^ =)

回答

1

由於described here,os.spawnv是一個不推薦的方法,您應該使用subprocess模塊。

除此之外,如果您總是返回1並且沒有進程產生,可能是由於到服務器代碼的錯誤路徑或服務器部分的錯誤代碼。如果您手動啓動服務器會發生什麼情況?

+0

服務器運行平穩,但我要嘗試子進程模塊,謝謝:) – Fabio