Quote:I want the device to both wait for a UDP packet, and also keep an eye on an IR signal
Multiprocessing is one way to do more than one thing at a time
https://pymotw.com/3/multiprocessing/basics.html Quote:and wait on the button press
Keep this in the program, i.e. not a separate process, as the program controls the terminal and input. A somewhat funky example from my toolbox that uses a Tkinter Button to terminate, and hopefully will help.
import tkinter as tk
import time
from multiprocessing import Process
class TestClass():
def test_f(self, sleep_time=0.5):
""" a simple counter function that simulates
a separate process
"""
ctr = 0
while True:
print(ctr)
ctr += 1
time.sleep(sleep_time)
def tk_quit(self):
""" this function just waits for a button click and then
exits/returns, allowing the rest of this program to execute
"""
root=tk.Tk()
tk.Button(root, text="Quit", command=root.quit,
width=10, bg="orange").grid()
root.mainloop()
if __name__ == '__main__':
## run function in the background
CT=TestClass()
## simulate 2 processes running different things
## start as many processes as you want
p1=Process(target=CT.test_f)
p1.start()
p2=Process(target=CT.test_f, args=(0.75,))
p2.start()
CT.tk_quit()
print("terminating processes")
for p in [p1, p2]:
if p.is_alive():
p.terminate()
p.join()