Which is the best way to close a program anytime by pressing Esc? I need to implement this thing in an important code, but my experiments didn't work.
This is the last one:
from multiprocessing import Process
import keyboard
import sys
def stop_anytime():
bool = True
while bool:
try:
if keyboard.is_pressed('Esc'):
sys.exit()
bool = False
except:
break
def print_numbers():
for n in range(150000):
print(n)
if __name__ == '__main__':
p1 = Process(target=stop_anytime)
p2 = Process(target=print_numbers)
p1.start()
p2.start()