使用功能运行游戏

问题描述 投票:0回答:1

我的代码:

import threading
import time 
import subprocess
Thread1 = threading.Thread(target=Run_exe)

def run_exe():
    subproccess.call("game.exe")

while True:
   time.sleep(3600)
   x = int(input(""))
   if x==1:
      Thread1.start()
   else:
      Thread1.join()

我的目标是:

  • 使用Run_exe函数运行游戏

  • 退出游戏时杀死线程

我不想从游戏本身退出游戏Python程序应该通过杀死线程来获取输入以退出游戏。

python subprocess python-multithreading
1个回答
1
投票

使用subprocess.Popen构造函数并保存pid:

process = subprocess.Popen("game.exe")
pid = process.pid

如果要停止该过程,请致电:(从this answer起]

import os
import signal

os.kill(pid, signal.SIGTERM) #or signal.SIGKILL 
© www.soinside.com 2019 - 2024. All rights reserved.