【问题标题】:Python close children subprocessPython关闭子子进程
【发布时间】:2013-07-19 09:09:51
【问题描述】:

我有一个非常简单的问题,关于在用 python 3 编写的脚本中杀死子子进程。

在哪里,

如果有的话

my_process = None

def open_storage():
    my_process = subprocess.Popen("waffles.exe")

def kill_children():
    my_process.kill()

调用open_storage()后,如果我调用kill_children(),我得到

AttributeError: 'NoneType' object has no attribute 'kill'

如果我有,

my_process = 无

my_process = subprocess.Popen("waffles.exe")

def kill_children():
    my_process.kill()

一切正常。

谁能解释这种奇怪的行为?我需要将open_storage() 作为一个函数,因为它被设计为由 tkinter 按钮触发。

谢谢。

【问题讨论】:

    标签: python windows web-services file web-applications


    【解决方案1】:

    需要使用全局变量,否则会使用局部变量。

    def open_storage():
        global my_process
        my_process = subprocess.Popen("waffles.exe")
    

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 2017-01-09
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 2017-03-01
      • 2013-08-20
      • 2021-12-06
      • 2019-07-09
      相关资源
      最近更新 更多