And a CPU that is stuck doing a lot of work over nothing as in this case is said to be spinning. This type of wait loop is often called a busy wait. So while the loop body is completely empty, Python is forced to continuously evaluate result is None, and actually, the fact that the loop is empty makes Python concentrate fully on repeating this evaluation as fast as it possibly can, burning a lot of CPU cycles, and making everything else running on that CPU much slower! This while-loop appears to be an empty loop, and in fact it mostly is, with the only exception that the loop exit condition needs to be checked over and over again to determine when the loop should exit. Look at CPU usage and note how it goes through the roof.
HOW TO MAKE SOURCE FILMMAKER RUN FASTER MAC
As soon as the script runs, open your Task Manager on Windows, Activity Monitor on Mac or maybe top if you prefer the command line. If you want to experience it, you can try the script on your system. # wait here for the result to be available before continuing If you want to try this, here is the complete script that you can copy/paste: from random import random The easiest and most intuitive way to perform this wait is to use a while-loop: # wait here for the result to be available before continuing In the following sections I'm going to show you a few different ways to implement this wait, starting from the worst and working my way up to the best. The version of this function that you see above does not have the waiting part implemented, you can see a TODO comment in the place where the wait needs to take place. The main application function starts the background calculation in a separate thread, then waits for the thread to complete its work and finally prints the result global variable. When the function reaches the end, a result global variable is set with the result of this made-up calculation, which is going to be, obviously, the number forty-two. To keep this example simple, I have coded this function with a time.sleep() call with a random time of up to 5 minutes. In this application, the background_calculation() function performs some computation that is slow. # TODO: wait here for the result to be available before continuing! Thread = threading.Thread(target=background_calculation) # when the calculation is done, the result is stored in a global variable To show you these wait patterns, I'm going to use an example application, shown below: from random import random I'm going to use Python for all the examples, but the concepts I'm going to present apply to all programming languages. 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 I'm going to show you a few different ways to wait.
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. For many types of applications, at times it is necessary to pause the running of the program until some external condition occurs.