Windows 7 64bit,python26
我运行一个Minecraft服务器,但有时程序因各种原因而停止运行。我希望在发生这种情况时启动一个新实例。
以下代码适用于notepad.exe,但即使之前没有停止,也会继续产生新的minecraft_servers。为什么?我该怎么办?
我是python的新手。
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 :(得分:3)
目前还不清楚为什么进程重启器涉及线程化。 这对你有用吗?
while True:
proc = subprocess.Popen("C:\\Users\\Bo\\AppData\\Roaming\\.minecraf\\Minecraft_Server.exe")
proc.wait()
print 'Minecraft_Server process terminated. Restarting now'
建议: