I’m trying to create a toggleable process (starts or ends when I press “r”). I know the process gets started, but I never receive any output from the process’s print statement. I’m new to multiprocessing but in my mind, the code should:
- wait for r to be pressed (stop program if p is pressed).
- if the process already exists, terminate and set to None (so I can call it repeatedly).
- if process does not exist, create process that will run record(), set to daemon so if I quit the program, the process also ends, and start process.
Any help or clarity on the problem would be greatly appreciated!
from pynput import keyboard as k import mulitprocessing as mp import os def record(): print("started record process") # will do things here def on_release(key): global record_process if key == k.KeyCode.from_char("p"): print("quitting") os._exit(0) if key == k.KeyCode.from_char("r"): if record_process == None: record_process = mp.Process(target=record, args=()) record_process.daemon = True record_process.start() # insanity check, make sure process gets started print(record_process.is_alive()) else: record_process.terminate() print("terminated process") record_process = None record_process = None key_list = k.Listener(on_release=on_release) key_list.start() while(True): pass