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. Localscope is easy to use and sophisticated … I continue to recommend Localscope.For many types of applications, at times it is necessary to pause the running of the program until some external condition occurs. The interface is remarkably easy to use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |