0

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?

Demetris
  • 2,981
  • 2
  • 25
  • 33

1 Answers1

1

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.

Community
  • 1
  • 1
dano
  • 91,354
  • 19
  • 222
  • 219
  • Thank you for you answer. The receiving rate of data is from 10 to 100Hz (updated the question). Is there a considerable delay when the thread will pass the data to the main thread? If reading the serial was in the main thread, there wouldn't be a need to pass the data. – Demetris Jul 18 '14 at 15:32
  • 1
    @Demetris Passing the received data between threads should be quite fast, because it's all happening within a single process. I'm not sure what is considered a "considerable" delay in this context, but I don't foresee it being a bottleneck. – dano Jul 18 '14 at 15:44