I am building a terminal shell program using tkinter
. The user is able to run any arbitrary program (not always python scripts). This is how I start the new process:
self.proc = subprocess.Popen("test.py", close_fds=False, shell=True, **get_std_child())
This is my test.py
program:
import time
try:
print("Start")
time.sleep(10)
print("End")
except KeyboardInterrupt as error:
print(repr(error), "!")
I am having trouble sending a Ctrl-c
event to the process. I am using Python 3.7.9. I tried all of these but none of them have the desired effect.
from signal import SIGINT, CTRL_C_EVENT, CTRL_BREAK_EVENT
proc.send_signal(SIGINT) # `ValueError: Unsupported signal: 2`
os.kill(proc.pid, SIGINT) # Doesn't do anything until I press it again then it throws: PermissionError: [WinError 5] Access is denied
os.kill(proc.pid, CTRL_C_EVENT) # SystemError: <built-in function kill> returned a result with an error set
os.kill(proc.pid, CTRL_BREAK_EVENT) # SystemError: <built-in function kill> returned a result with an error set
According to the signal
documentation SIGINT
as well as CTRL_C_EVENT
should work on Windows.
The desired effect is the same as what happens to cmd when I press Ctrl-c
:
C:\Users\TheLizzard\Documents\GitHub\Bismuth-184\src>python test.py
Start
KeyboardInterrupt() !
C:\Users\TheLizzard\Documents\GitHub\Bismuth-184\src>
PS: The full code is here but it's very long.