So, I want to make a program that execute associated part of code while and until the key on keyboard is pressed. I was looking for it long, long time and I didn't find anything. Can you please help me or just send something like a tutorial ? I want the program to do (for example) on "W" press do , on "S" press do .
There are many posts that address this. Here is a link to get you started.
Here is a python program that will do what you ask. VERY IMPORTANT - don't use more multiprocessing such as children of workers, queues, etc because the call to terminate will mess up garbage collection and will cause you problems over time.
import multiprocessing as mp import time import sys import tty import termios def get_ch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch def worker(): """ Do work """ while True: print("Working...") time.sleep(2) p = mp.Process(target=worker) p.start() get_ch() p.terminate()
Hope this helps!