2011-10-23 30 views
0

Windows 7 64位,python26python。如果程序停止,必須啓動一個新實例

我運行一個minecraft服務器,但有時程序因爲不同的原因停止運行。我希望在發生這種情況時啓動一個新實例。

下面的代碼適用於notepad.exe,但即使前一個沒有停止,它仍會產生新的minecraft_servers。爲什麼?我該怎麼做?

我是新來的蟒蛇。

import Queue, thread, subprocess, time 

results= Queue.Queue() 
def process_waiter(popen, description, que): 
    try: popen.wait() 
    finally: que.put((description, popen.returncode)) 
process_count= 0 

while True: 
    proc1= subprocess.Popen("C:\\Users\\Bo\\AppData\\Roaming\\.minecraf\\Minecraft_Server.exe") 
    thread.start_new_thread(process_waiter, 
    (proc1, "1 finished", results)) 
    process_count+= 1 

    time.sleep(10) 

    while process_count > 0: 
     description, rc= results.get() 
     print "job", description, "ended with rc =", rc 
     process_count-= 1 
+0

我認爲問題與該程序產生一個新的過程中做。 – user1009591

回答

3

目前還不清楚爲什麼線程涉及進程重新啓動程序。 這是否對你的工作:

while True: 
    proc = subprocess.Popen("C:\\Users\\Bo\\AppData\\Roaming\\.minecraf\\Minecraft_Server.exe") 
    proc.wait() 
    print 'Minecraft_Server process terminated. Restarting now' 

建議:

  • 開始與該作品的幾行代碼,然後在代替線程
  • 添加功能
  • 使用線程
  • 由於它與子流程中函數的名稱相似,因此不會命名變量「popen」。
  • 添加日誌記錄或打印語句,所以你可以告訴腳本是做
  • 監控進程創建與任務管理器
+0

這對我不起作用。它不會等待,但產生多個實例。它確實如何處理notepad.exe。 – user1009591

相關問題