Reputation: 3241
I am building an application in Python and TkInter, which accepts a constant stream of data through the serial port of the PC, at about 10-100Hz (i.e. a packet of data will arrive every 10-100ms). This data is then processed and presented to the user.
A simple implementation would be to have a big loop, where the new values are received through the serial and then the GUI is updated.
Another implementation which I find more robust, would be to have a separate thread to handle the incoming data and then send the data to the main application to update the GUI. I am not sure how to implement this, though.
Would the separate thread give any benefit in this situation?
Upvotes: 0
Views: 142
Reputation: 94881
Yes, a separate thread would definitely be beneficial here, because the call you use to accept serial data will most likely block to wait for data to arrive. If you're using just a single thread, your entire GUI will freeze up while that call blocks to wait for data. Then it will just briefly unfreeze when data is received and you update the UI, before being frozen again until more data arrives.
By using a separate thread to read from the serial port, you can block all you want without ever making your GUI unresponsive. If you search around SO a bit you should be able to find several questions that cover implementing this sort of pattern.
Also note that since your thread will primarily be doing I/O, you should not be noticeably affected by the GIL, so you don't need to worry about that.
Upvotes: 1