Threading: Instead of using a global variable to control the threads, consider using a threading library to control the threads in a more efficient and thread-safe way. Here is an example of how you could modify your code to use the threading library:
def listenThread():
while keep_running:
listenCommand()
listen_thread = threading.Thread(target=listenThread, daemon=True)
listen_thread.start()
def flushThread():
while keep_running:
for buffer in bufferlist:
if buffer.buffer != "S":
buffer.flush()
time.sleep(0.1)
flush_thread = threading.Thread(target=flushThread, daemon=True)
flush_thread.start()
In this example, the listenThread function and the flushThread function are run on separate threads using the Thread class from the threading library.
Add threading by doing this (chatgpt)
Threading: Instead of using a global variable to control the threads, consider using a threading library to control the threads in a more efficient and thread-safe way. Here is an example of how you could modify your code to use the threading library:
In this example, the listenThread function and the flushThread function are run on separate threads using the Thread class from the threading library.