Example analysis of multiple timed tasks developed by Python in single-threaded execution

Source: Internet
Author: User

single-threaded multi-timer tasks

1. Initial version:

Train of thought: Timer, plainly is the delay to execute the specified program, the current self-reconfigurable python inside the timer is not too realistic, the ability to reach, so the delay operation also need to use the system timer, but we can change the rules; Add all the programs that you want to schedule to a specific list, Take out the shortest time program in the list and carry out the threading. Timer (Time,callback) binding, such as time-out trigger custom callback, execute just list out of the program, and then update the time, again the list of the shortest time the program took out, continue to threading. Timer bindings, continuous iteration loops, and when new scheduled tasks are added to the list, the current threading. The timer binding cancels, updates the time in the list, takes out the shortest time again, carries on the threading. Timer binding ...

Code:


Import threadingimport Timeclass timer (): "Single-threaded Timer" "Def __init__ (self): self.queues = [] Self.tim ER = None self.last_time = Time.time () def start (self): item = Self.get () if item:self . Timer = Threading. Timer (Item[0],self.execute) Self.timer.start () def add (Self,item): Print (' Add ', item) self.flus H_time () self.queues.append (item) Self.queues.sort (Key=lambda x:x[0]) if self.timer:self. Timer.cancel () Self.timer = None Self.start () def get (self): item = None If Len (Self.qu eues) > 0:item = self.queues[0] Return item def pop (self): item = None If Len (self. Queues) > 0:item = Self.queues.pop (0) return item def flush_time (self): Curr_time = time.t     IME () for i in self.queues:i[0] = i[0]-(curr_time-self.last_time) Self.last_time = Curr_time DEF Execute (self): # if Self.timer: # Self.timer.cancel () # Self.timer = None item = SE Lf.pop () self.flush_time () If item:callback = item[1] args = item[0] Callb Ack (args) Self.start ()

Execution and output:


if __name__ = = ' __main__ ': # Number of Detection Threads def func (): While True:print (Threading.active_count ()) Time.sleep (1) F1 = Threading. Thread (Target=func) F1.start () Import logging logging.basicconfig (level=logging.info,format= "% (asctime) s% (Me Ssage) S ", datefmt="%m/%d/%y%h:%m:%s [%A] ") def func1 (*args): Logging.info (' func1%s '%args) # time.sleep ( 5) def FUNC2 (*args): Logging.info (' func2%s '% args) # Time.sleep (5) def func3 (*args): Logg        Ing.info (' func3%s '% args) # Time.sleep (5) def func4 (*args): Logging.info (' func4%s '% args)    # Time.sleep (5) def func5 (*args): Logging.info (' func5%s '% args) # time.sleep (5) # test T1 = Timer () logging.info (' Start ') t1.add ([5,func1]) time.sleep (0.5) T1.add ([4,func2]) Time.sleep (0.5) t 1.add ([3,func3]) time.sleep (0.5) T1.add ([2,func4]) time.sleep (0.5) T1.add ([1,func5]) TIME.SLeep (5) T1.add ([1,func1]) T1.add ([2,func2]) T1.add ([3,func3]) T1.add ([4,func4]) T1.add ([5,func5]) # Output # 2 # 07/27/2017 10:36:47 [Thursday] Start # Add [5, <function func1 at 0x000000d79fc77e18>] # Add [4  , <function Func2 at 0x000000d79fca8488>] # 3 # Add [3, <function func3 at 0x000000d79fca8510>] # Add [2, <function func4 at 0x000000d79fca8598>] # 3 # Add [1, <function func5 at 0x000000d79fca8620>] # 3 # 07/27/2017 10:36:50 [Thursday] Func5 1 # 07/27/2017 10:36:51 [Thursday] Func4 0.498349666595459 # 3 # 07/2    7/2017 10:36:51 [Thursday] func3 0.49782633781433105 # 07/27/2017 10:36:52 [Thursday] Func2 0.49848270416259766 # 3 # 07/27/2017 10:36:52 [Thursday] func1 0.48449039459228516 # 2 # 2 # Add [1, <function func1 at 0x000000d79 Fc77e18>] # Add [2, <function func2 at 0x000000d79fca8488>] # Add [3, <function func3 at 0x000000d79fca8 510>] # Add [4, &LT;function func4 at 0x000000d79fca8598>] # Add [5, <function Func5 at 0x000000d79fca8620>] # 3 # 07/27/2 017 10:36:55 [Thursday] func1 0.9990766048431396 # 3 # 07/27/2017 10:36:56 [Thursday] Func2 0.9988017082214355 #    3 # 07/27/2017 10:36:57 [Thursday] func3 0.99928879737854 # 07/27/2017 10:36:58 [Thursday] Func4 0.9991350173950195 # 3 # 3 # 07/27/2017 10:36:59 [Thursday] Func5 0.9988160133361816

Execute code

Note: Check the code output, all the timers are executed according to the time of calibration, very perfect, everything looks very good, just look, oh da, when you put the Func inside the Time.sleep (5) enabled, the number of threads rub up up; The reason is the last timer callback or executed , the next timer has been started, then there is a new thread, hey, failed

2. Revised version

Idea: Using the creator consumer model, threading. condition the condition variable; Forcing is always enabled is a timer!

Code:


Import Timeimport threadingimport loggingclass Newtimer (threading. Thread): "Single-threaded Timer" ' Def __init__ (self): Super (). __init__ () self.queues = [] Self.timer = No NE self.cond = threading.             Condition () def run (self): while True: # print (' Newtimer ', self.queues) Self.cond.acquire () item = Self.get () callback = None if not item:logging.info (' Newtimer Wai                T ') self.cond.wait () elif item[0] <= time.time (): New_item = Self.pop ()                callback = new_item[1] Else:logging.info (' newtimer start sys timer and wait ') Self.timer = Threading. Timer (Item[0]-time.time (), Self.execute) Self.timer.start () self.cond.wait () self. Cond.release () if Callback:callback (Item[0]) def add (self, item): # Print (' Add ', item ) Self.cond. Acquire () item[0] = item[0] + time.time () self.queues.append (item) self.queues.sort (Key=lambda x:x[ 0]) Logging.info (' Newtimer add notify ') if Self.timer:self.timer.cancel () Self.timer = None self.cond.notify () self.cond.release () def pop (self): item = None If Len (self.queues ) > 0:item = Self.queues.pop (0) return item def get (self): item = None If Len (self. Queues) > 0:item = self.queues[0] Return item def execute (self): Logging.info (' Newtimer ex Ecute notify ') Self.cond.acquire () self.cond.notify () self.cond.release ()

Execution and output:


if __name__ = = ' __main__ ': def func (): While True:print (Threading.active_count ()) TIME.SL EEP (1) F1 = Threading. Thread (Target=func) F1.start () logging.basicconfig (level=logging.info,format= "% (asctime) s% (message) s", datefmt= "% M/%d/%y%h:%m:%s [%A] ") Newtimer = Newtimer () newtimer.start () def func1 (*args): Logging.info (' func1%s '%A RGS) Time.sleep (5) def FUNC2 (*args): Logging.info (' func2%s '% args) Time.sleep (5) def func3 (*a RGS): Logging.info (' func3%s '% args) Time.sleep (5) def func4 (*args): Logging.info (' func4%s '% AR GS) Time.sleep (5) def func5 (*args): Logging.info (' func5%s '% args) time.sleep (5) Newtimer.add (    [5,FUNC1])    Newtimer.add ([4,FUNC2]) Newtimer.add ([3,func3]) Newtimer.add ([2,func4]) Newtimer.add ([1,func5]) Time.sleep (1)  Newtimer.add ([1,func1]) Newtimer.add ([2,func2]) Newtimer.add ([3,func3]) Newtimer.add ([4,func4])  Newtimer.add ([5,func5]) # output # 07/27/2017 11:26:19 [Thursday] newtimer wait# 07/27/2017 11:26:19 [Thursday] Newtimer AD D notify# 07/27/2017 11:26:19 [Thursday] newtimer add notify# 07/27/2017 11:26:19 [Thursday] newtimer add notify# 07/27/20 11:26:19 [Thursday] newtimer add notify# 07/27/2017 11:26:19 [Thursday] newtimer add notify# 07/27/2017 11:26:19 [Thurs Day] newtimer start sys timer and wait# 07/27/2017 11:26:20 [Thursday] Newtimer execute notify# 4# 07/27/2017 11:26:20 [Th Ursday] Func5 1501125980.2175007# 07/27/2017 11:26:20 [Thursday] newtimer add notify# 07/27/2017 11:26:20 [Thursday] NewTi Mer add notify# 07/27/2017 11:26:20 [Thursday] newtimer add notify# 07/27/2017 11:26:20 [Thursday] newtimer Add notify# 07 /27/2017 11:26:20 [Thursday] newtimer add notify# 3# 3# 3# 3# 3# 07/27/2017 11:26:25 [Thursday] Func4 1501125981.2175007#  3# 3# 3# 3# 07/27/2017 11:26:30 [Thursday] func1 1501125981.218279# 3# 3# 3# 3# 3# 3# 07/27/2017 11:26:35 [Thursday] Func3 1501125982.2175007# 3#3# 3# 3# 07/27/2017 11:26:40 [Thursday] func2 1501125982.218279# 3# 3# 3# 3# 3# 07/27/2017 11:26:45 [Thursday] Func2 15011 25983.2175007# 3# 3# 3# 3# 3# 07/27/2017 11:26:50 [Thursday] func3 1501125983.218279# 3# 3# 3# 3# 3# 07/27/2017 11:26:55 [ Thursday] func1 1501125984.2175007# 3# 3# 3# 3# 3# 07/27/2017 11:27:00 [Thursday] func4 1501125984.218279# 3# 3# 3# 3# 3# 07/27/2017 11:27:05 [Thursday] func5 1501125985.218279# 3# 3# 3# 3# 3# 07/27/2017 11:27:10 [Thursday] NewTimer wait

Output

Note: This time regardless of the number of test threads will not rub against the rise, but also to achieve the multi-timer task requirements; Disadvantage: the use of two threads, no single-threaded to achieve, the second time the accuracy of the problem, need to wait for the last timer program execution, the program can continue to run

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.