I am developing a Python application that needs to access a machine to perform some (long) tasks. An asynchronous module seems like a good choice for anything connected to the network, but now I need to access the serial port for one specific component. I have implemented some level of abstraction for the actual material of the serial port, but I cannot figure out how to intelligently integrate it with asynchronous.
Next setup: I have a loop loop that regularly talks to the machine and decodes the responses. Using the enqueue_query() method, I can put a query string in a queue, which will then be sent to the computer by another thread and will trigger a response. By passing to threading.Event (or something with the set() method), the caller can perform a pending response block. It might look something like this:
f = threading.Event() ch.enqueue_query('2 getnlimit', f) f.wait() print(ch.get_query_responses())
Now my goal is to put these lines in a coroutine and handle the asynchronous wait so that the application can do something else. How can i do this? This will probably work by wrapping f.wait() in Executor, but it seems a little silly as it will create a new thread to wait for another thread to do something.
Thanks! Best regards, Philipp
source share