【问题标题】:Running games using functions使用函数运行游戏
【发布时间】:2019-10-02 13:27:25
【问题描述】:

我的代码:

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 运行游戏并使用 python 退出它,而不会干扰使用函数和线程的游戏
  • 你为什么使用线程?

标签: python subprocess python-multithreading


【解决方案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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多