python如果程序停止,则必须启动一个新实例
Windows 7 64位,Python 26 我运行一个minecraft服务器,但有时程序会因为不同的原因而停止。我希望在发生这种情况时启动一个新实例 下面的代码适用于notepad.exe,但不断生成新的minecraft_服务器,即使前一个服务器没有停止。为什么?我该怎么办 我是python新手python如果程序停止,则必须启动一个新实例,python,multithreading,process,wait,execute,Python,Multithreading,Process,Wait,Execute,Windows 7 64位,Python 26 我运行一个minecraft服务器,但有时程序会因为不同的原因而停止。我希望在发生这种情况时启动一个新实例 下面的代码适用于notepad.exe,但不断生成新的minecraft_服务器,即使前一个服务器没有停止。为什么?我该怎么办 我是python新手 import Queue, thread, subprocess, time results= Queue.Queue() def process_waiter(popen, descript
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
目前尚不清楚进程重启程序中为什么会涉及线程。 这对你有用吗:
while True:
proc = subprocess.Popen("C:\\Users\\Bo\\AppData\\Roaming\\.minecraf\\Minecraft_Server.exe")
proc.wait()
print 'Minecraft_Server process terminated. Restarting now'
建议:
- 从几行有效的代码开始,然后添加功能
- 在中使用线程而不是线程
- 不要将变量命名为“popen”,因为它与子流程中函数的名称相似
- 添加日志记录或打印语句,以便您可以知道脚本正在执行的操作
- 使用任务管理器监视流程创建
while True:
proc = subprocess.Popen("C:\\Users\\Bo\\AppData\\Roaming\\.minecraf\\Minecraft_Server.exe")
proc.wait()
print 'Minecraft_Server process terminated. Restarting now'
建议:
- 从几行有效的代码开始,然后添加功能
- 在中使用线程而不是线程
- 不要将变量命名为“popen”,因为它与子流程中函数的名称相似
- 添加日志记录或打印语句,以便您可以知道脚本正在执行的操作
- 使用任务管理器监视流程创建