Posted by Miguel Grinberg this article explains various (in)correct ways of implementing threads for time when it is necessary to pause the running of the program until some external condition occurs.
You may need to wait until another thread finishes, or maybe until a new file appears in a directory on disk that is being watched.
In these and many other situations you will need to figure out a way to make your script wait, and this isn’t as easy as it sounds if you want to do it properly! In this article author shows you a few different ways to wait.
The author then shows you a few different ways to implement this wait, starting from the worst and working the way up to the best:
- The ugly: busy waiting
- The bad: busy waiting with sleep
- The good #1: joining the thread
- The good #2: waiting on an event
- The good #3: waiting while displaying a progress percentage
In most cases, the operating system provides efficient wait mechanisms, so all you need to do is find how to access those from Python. Refreshing read!
[Read More]