Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading best practice
#1
I have a small program that works, I want to expand it but will need to run multiple thing simultaneously. I am after some advice as to best practice (and anything else that will catch a beginner out)

What I have currently is running on a raspberry PI using 2 switches on the GPIO pins counting how often they are triggered and updating a GUI (tkinter).

I want to expand it to include another couple of sensors (optoreflectors) they will be doing something similar (count and time) but may trigger at the same time as the other switches. They will also update the same GUI.

Reading around I am assuming I need to run both processes as threads. What is best practice way of doing this? My assumption is a module containing the GUI and the thread info then separate modules for each thread. with the threads updating global variables and the GUI looping and updating a couple of times a second (maybe slower)

There seems to be a few approaches to global variables between modules, from a best practice and future proofing point of view what route should I be going down?
Should I be splitting the GUI to a separate thread as well?

I have done some coding before but it was a very long time ago (15 plus years) and not python so I am essentially starting learning from scratch so simple answer would be appreciated.

Thanks
Reply
#2
There is no reason you have to use multiple modules. If the code is short I use one module. It makes it easiest to distribute. If the code is large, break it up into modules such that each module has a well defined role.

I avoid using globals as much as possible. I write moderate sized machine and process control applications with no global variables at all. Of course I cheat. I use classes and they let me share information in a global variable like way but with better syntax

You do not need to use threads unless something blocks execution. Threads do not let your program do two things at the same time, they just let your program do something else when it would otherwise be waiting. This is why threads work great for network applications or file processing applications because they are loaded with little sleep periods where the program is waiting for a response to some request. Applications that are CPU intensive may run slower when multi-threaded because of the overhead associated with managing the threads. It sounds like your program does a lot of waiting for some input event to occur. Your program may benefit from having threads if the event processing takes lots of time and the processing may have to wait for resources. Your code would benefit a lot if you are blocking while waiting for these input events.

I would try to first write the program without threads. If you encounter problems it is pretty easy to add threading later.
Reply
#3
Thank you. Will do so more reading about class, I hadnt understood the sharing of results from them properly assumed they just used variables, I've not needed to use them.

Regarding not threading it I agree its better not to, my code is short and in coding terms has wait time so overhead isnt a concern but I am expecting a high likelihood of interrupt calls being almost simultaneous I cant see a way of writing the code that will accept that, although I am not sure how python would handle an interuppt mid execution? I might be able to sequence it in such a way that it looks for interupts in a specific order and waits for each one. But I need to have a longer think about the hardware, how thats triggered and what output I want to decide if that will create other problems.

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best practice on using virtual environments in Python bytecrunch 6 855 Feb-14-2024, 03:22 PM
Last Post: snippsat
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,861 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,130 Oct-01-2021, 08:32 PM
Last Post: muzikman
  best practice for import libraries and using pyinstaller aster 3 2,880 Apr-17-2021, 11:12 AM
Last Post: snippsat
  How to properly close db connection, best practice t4keheart 6 3,031 Jan-24-2020, 06:58 PM
Last Post: Marbelous
  Help with string practice Hermann_Fegelein 2 2,691 Aug-15-2018, 04:56 PM
Last Post: Hermann_Fegelein
  Best Practice For String Quotations ? Zork_3 9 49,972 Sep-01-2017, 07:16 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020