Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python如果程序停止,则必须启动一个新实例_Python_Multithreading_Process_Wait_Execute - Fatal编程技术网

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

Windows 7 64位,Python 26

我运行一个minecraft服务器,但有时程序会因为不同的原因而停止。我希望在发生这种情况时启动一个新实例

下面的代码适用于notepad.exe,但不断生成新的minecraft_服务器,即使前一个服务器没有停止。为什么?我该怎么办

我是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

目前尚不清楚进程重启程序中为什么会涉及线程。 这对你有用吗:

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”,因为它与子流程中函数的名称相似
  • 添加日志记录或打印语句,以便您可以知道脚本正在执行的操作
  • 使用任务管理器监视流程创建

这对我不起作用。它不等待,而是生成多个实例。它是如何与for notepad.exe一起工作的。这对我不起作用。它不等待,而是生成多个实例。它如何与for notepad.exe一起工作。我假设问题与生成新进程的程序有关。我假设问题与生成新进程的程序有关。