Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python close children subprocess

If I have

my_process = None

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

def kill_children():
    my_process.kill()

After calling open_storage(), if I call kill_children(), I get

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

But if I have,

my_process = None

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

def kill_children():
    my_process.kill()

Everything works fine.

Can anyone explain this strange behaviour? I need to have open_storage() as a function because it's designed to be triggered by a tkinter button.

like image 802
William Yang Avatar asked Aug 07 '26 18:08

William Yang


1 Answers

You need to use global, otherwise it will use a local variable.

def open_storage():
    global my_process
    my_process = subprocess.Popen("waffles.exe")
like image 95
nom-mon-ir Avatar answered Aug 10 '26 09:08

nom-mon-ir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!