This month I wanted to start learning python, and I have went threw the basics of learning the language pretty fast. However now I am starting to scratch the surface when it comes to the wide range of standard libraries that there are to work with. One library that I think I should at least write a few quick examples with at least would be the threading library.
So then the Timer class is a good way to just delay the calling of a function for a few seconds. I should then also be able to use this to start a basic main app loop also. So lets look at just a few more examples of the Timer class in action, and maybe even take a quick look at some other options to work with in the threading library.
I am not sure if the Timer Class might be the best option to create this kind of loop though. It would seem that there are some more options in the threading library that I should take a quick look at least. However it would seem that something as simple as this might still work just fine when it comes to some basic application examples.
Another major function in the threading library might be the Thread class. It would seem that this class can help when it comes to having more than on Thread running at the same time. It is not to hard to confirm this, say I have a function called heavy that just does something that might take a little while, such as printing something to the standard output a thousand times. If I just call this heavy function twice when will happen is that the first set of a thousand print calls will log to the console first, and then the second set will start. This is because I am calling the heavy function twice in the same thread, so the first call must complete until the second set can then start.
However if I use the Thread class of the threading library I can call my heavy function in a separate thread. Then I can call the heavy function again in the main thread. The result is then having both calls happening at the same time.
So then the Thread Class in the threading module is there to work with if I ever want to do real threading with python.
I could take some time to see about making a real serious project of some kind with python that involves the use of a main app loop of one kind or another. In time maybe I will get around to doing just that, however for now maybe I can just quickly put together a basic simple script that will just keep counting until the script is killed.
This example makes use of the global keyword as a way to update a simple state outside the body of a function in the from of just a single variable. Inside the loop function I just keep stepping that loop forward by one each time the loop is function is called.
When I call this script I just keep getting a number logged out to the console each second the just goes up by one each second. Nothing to write home about, however when it comes to starting to get the hand of this sort of thing in python I need to start somewhere.
When it comes to making an app loop one way or another I like to have an about of time that has passed sense the last time the function was called. This value that is an amount of seconds that has when by can then be used as a way to update the value of a variable by a delta rate per second.
So that is all for my post on the python threading library for now at least. I think that I might need to start thinking in terms of what I might want to do with python in the long term moving forward though. That is that I should start thinking about what I might want to do when it comes to some actual real python examples that might make use of the threading library. In that event I am sure that I will end up coming back to what I have wrote here to expand the content more.
There is a whole lot more to write about when it comes to this library on threading in python, but I just wanted to touch base on this library for now. The reason why is that I might sometimes be in a situation in which I might want to have more than one thread running at the same time to help speed up the process of something.