After one year, I began to face graduation. The graduation season is happy, but it is also urgent. In February, I started planning my graduation thesis. Till now, my thesis has basically been completed. I feel that I have studied my graduation thesis in the lab for the past two years. When books are used, they hate less, and spend a lot of energy in the days of research papers in the laboratory. When I write a thesis and then look back at my documents, I suddenly found that it was a serious mistake not to take a detailed paper and study note. As a result, the process of writing a thesis becomes to read the document and write a short article. After the paper is completed, I found that I had a new understanding of the new content, and I had a new understanding of many parts that I did not understand before!
Well, the above is an overview of my recent life. In late February, someone saw an article about SmartAPI in my blog. Then, they contacted me via QQ and asked me to help write a small program, it is used to obtain the weather conditions of all cities provided by the API in real time, so I spent about three days writing a demo in that week, A preliminary achievement was made and the program was relatively stable.
Today I will share some of them with you. The customer needs to collect real-time weather forecasts every two hours, so I need to write a simple provisioning program to complete this task, that is, a daemon process is required to timing the tasks, the relevant program is automatically called at the specified interval to complete the data collection process.
Development Environment: Windows8.1 + Python2.7
Okay. Let's talk about the code first:
threading = = + = next_day === struct.tm_mday + 1 = = main(loc, istomorrow=1 8, 0, 0, istomorrow), 13 * 60 * 60 == len(sys.argv) < 2 len(sys.argv) == 211], sys.argv[2])
In Python, the standard library provides a module for scheduled execution of a task, namely, sched and Timer classes. The sched module is a scheduling (latency processing mechanism). Each time you want to regularly execute a task, you must write a scheduler.
The procedure is as follows:
(1) generate the Scheduler:
s = sched.scheduler(time.time,time.sleep)
The first parameter is a function that returns a timestamp, and the second parameter can be blocked before the scheduled time reaches. It can be said that the designer of the sched module is "playing a huge game". For example, the first function can be a custom function, not necessarily a timestamp, and the second function can also be a socket block.
(2) Add a scheduling event
In fact, there are enter and enterabs. We use enter as an example.
s.enter(x1,x2,x3,x4)
The four parameters are respectively: interval event, priority (used for the simultaneous execution of two events at the same time), and The called trigger function, respectively. (Note: be sure to use tuple for example. If there is only one parameter(Xx ,)).
(3) Run
s.run()
By analyzing the above program, we can know that our goal is to detect the system time. When the system time reaches the critical point at a.m., the program is triggered. This aims to facilitate the program to run up the night before and get the first batch of data at the next day.
So, what is the role of the Timer module? To put it bluntly, it is a Timer, a program cycle Timer, and a collection operation is executed every time.
My thesis has been written recently. The next step is to write the scheduling program as a Windows Service and start it on your own. This is more convenient!