2

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)
4

1 に答える 1

2

次のような簡単なことを行うことで、プラットフォームに依存しない呼び出しを簡単に行うことができます。

try:
    import win32
    def kill(param):
        # the code from S.Lotts link
except ImportError:
    def kill(param):
        # the unix way

これがデフォルトでPythonに存在しない理由はわかりませんが、プラットフォームに依存しないlib(または少なくともwin + mac)を作成するのがそれほど難しくないファイル変更通知など、他の領域にも非常によく似た問題があります+linux)。オープンソースだと思うので、自分で修正する必要があります:P

于 2009-11-05T18:15:58.260 に答える