Windows で subprocess.Popen.terminate() または kill() コマンドを使用してプロセスを強制終了しようとすると、アクセス拒否エラーが発生します。ファイルが存在しなくなった場合にプロセスを終了するためのクロスプラットフォームの方法が本当に必要です(はい、それが私がしていることを行う最もエレガントな方法ではないことはわかっています)、プラットフォーム呼び出しを使用する必要はありませんまたは可能であれば、win32api をインポートします。
また、タスクを強制終了すると、ライブラリのその部分の反復を削除できるはずですよね? (何かに取り組み、その作業中に変更する予定がある場合は、スライスを使用する必要があることについて読んだことを覚えていますか?)
#/usr/bin/env python
#import sys
import time
import os
import subprocess
import platform
ServerRange = range(7878, 7890) #Range of ports you want your server to use.
cmd = 'VoiceChatterServer.exe'
#********DO NOT EDIT BELOW THIS LINE*******
def Start_IfConfExist(i):
if os.path.exists(str(i) + ".conf"):
Process[i] = subprocess.Popen(" " + cmd + " --config " + str(i) + ".conf", shell=True)
Process = {}
for i in ServerRange:
Start_IfConfExist(i)
while True:
for i in ServerRange:
if os.path.exists(str(i) + ".conf"):
res = Process[i].poll()
if not os.path.exists(str(i) + ".conf"): #This is the problem area
res = Process[i].terminate() #This is the problem area.
if res is not None:
Start_IfConfExist(i)
print "\nRestarting: " + str(i) + "\n"
time.sleep(1)