Quantcast
Viewing all articles
Browse latest Browse all 4825

Python • Re: KeyboardInterrupt in While True statement

I guess you're using os.system. Don't use it.

Sample to demonstrate:

Code:

import osimport subprocessdef video1():    # blocking call    # grabs stdin, stdout, stderr and KeyboardInterrupt    # simply, never use it!    os.system("mpv Downloads/VIDEO.mkv")def video2():    # blocking call    # grabs stdin, stdout, stderr, but does not catch KeyboardInterrupt    # the standard way to run a program and waiting for it to finish    subprocess.run(        ["mpv", "Downloads/VIDEO.mkv"]    )try:    # calling the function video1 blocks    # and also the signal KeyboardInterrupt is caught by the    # os.system call    video1()except KeyboardInterrupt:    # this never prints    print("-== KeyboardInterrupt ==-")finally:    print("Video1 finished")try:    # blocking call    # but does not catch KeyboardInterrupt    video2()except KeyboardInterrupt:    print("-== KeyboardInterrupt ==-")finally:    print("Video2 finished")

Statistics: Posted by DeaD_EyE — Sun Jan 19, 2025 9:24 pm



Viewing all articles
Browse latest Browse all 4825

Trending Articles