I have a program, which needs to continuously run in the background, but be able to receive instructions to change. I have this thread running, which sends data to an Arduino and receives data back:
class receiveTemp (threading.Thread):
def __init__(self, out):
threading.Thread.__init__(self)
self.out = out
def run(self):
self.alive = True
try:
while self.alive:
rec = send("command")
self.out.write(rec)
except BaseException as Error:
print(Error)
pass
Now I need to change the command I send with an external program.
I tried using Pyro4, but I can not seem to get a Thread running on the server and then controlling it with the client.
Any ideas?
Scott Mermelstein's advice is good, I hope you will look into interprocess communications. But as a quick example to get you started, I would suggest modifying your code like this:
This code uses a
threading.Event
as a signal to the thread that it should stop. It then uses aqueue.Queue
as a way to send commands to the thread from outside. You will need to useq.put(command)
to add commands to the queue from outside the thread.I didn't test this with an Arduino, I created my own version of
send
for testing that just returned the command.