This article addresses the thread control of two scenes
1. Thread Delay: delay a predetermined time, and then executing the next
2. The two threads, when a thread execution time exceeds the allotted time, another thread execution
Scenario 1: Timer
With specific reference to the follow-up will write my blog
Scenario 2: multi-threaded base class inheritance
DelayAction: rewriting run method, the delay in the run
DelayAction2: rewriting run method, the delay in the run, and obtain an output
class DelayAction (threading.Thread): # delay the implementation of a function DEF __init__ (Self, sec, FUNC, * args): . threading.Thread __init__ (Self) self.sec = sec self.func = FUNC self.args = args DEF RUN (Self): the time.sleep (self.sec) the Apply (self.func, self.args) class DelayAction2 (threading.Thread): # delay the implementation of a function, and to capture the return DEF __init__ (Self, sec , FUNC, * args): threading.Thread. __init__(self) self.sec = sec self.func = func self.args = args self.res = None def run(self): time.sleep(self.sec) self.res = apply(self.func, self.args) if __name__ == '__main__': ### test DelayAction def myfunc(x): print(x) return 1 da = DelayAction(10, myfunc, 222) da.start() # Print 222 after 10s # ## Test DelayAction2 da2 = DelayAction2 (10, myfunc, 333 ) da2.start () # print 333 after 10s Print (da2.res) # immediately play None, "print" is a parallel thread and da2 the reason why print out None, rather than a function value, because the function has not enforce it da2.join () Print (da2.res) # after the band finished function, print out 1
Scene 1 and 2: decorative implemented
timeout: simple example, to facilitate comparison subsequent
timeout2: timer to delay execution of a callback function, however, was decorated function and not be delayed, if the delay is realized together with t.join
timeout3: solve such a scenario: I need to perform a function, if not over perform this function within a predetermined time, the forced end, similar to the web in wait
// This is an example of failure , return the callback, the end does not make the entire function
timeout4: to solve the above scenario, add a thread daemon
def timeout (FUNC): # Decorative function is decorated with parameters def myfunc (sec): the time.sleep (sec) FUNC () return myfunc def timeout2 (sec, the callback): # decorator decorator with parameters def _deco (FUNC): t = Threading.Timer (sec, callback) # Threading.Timer timer, sec seconds after the callback function t.start () t.join () DEF myfunc (Arg): # Note that in fact did not delay myfunc, this is just a sample, you can expand according to their own needs, such as a line in the function plus t.join () FUNC (Arg) return myfunc return _deco DEFtimeout3 (sec, callback): # there is such a scenario: I need to perform a function, if not over perform this function within a predetermined time, the forced end of this function not implemented [demand] DEF _deco (FUNC): t = Threading.Timer (sec, callback) # callback assume the end of the 10s t.start () DEF myfunc (): FUNC () # garnished function assumes that the end of the 100s return myfunc return _deco DEF timeout4 (callback, Arg): # there is such a scene: I need to perform a function, if not over perform this function within a predetermined time, the forced end [] the need to achieve this function DEF _deco (FUNC): t = threading.Thread (target = callback, args = ( Arg,)) # callback assume the end of the 10s t.setDaemon (True) # # must, daemon thread, the main thread myfunc equivalent t.start () DEF myfunc (): FUNC () # garnished function assumes end 100s return myfunc return _deco IF the __name__ == ' __main__ ' : # ## Test timeout @Timeout DEF Test (): Print (33 is ) # Test (10) # 33 is printed after 10s # ## Test timeout2 DEF the callback (): Print ( ' callback2' ) @ Timeout2 ( 10 , callback) DEF test2 (the X-): Print (the X-) # test2 (100) immediately print out # 100, test2 is immediately executed, and the delay is not # print out callback2 after 10s, the callback function to delay execution # # timeout2 if coupled with the t.join, 10s immediately have printed callback2 100, which is double the delay # ## Note understanding on the line, to see how to write the actual needs of # ## the Test timeout3 DEF callback () : Print ( ' callback3 ' ) return @ timeout3 ( 10 , the callback) DEF Test3 (): the time.sleep ( 100 ) Print ( ' Test3 ' ) # Test3 () # print out after 10s after callbak2 callback3,100s print out Test3 # # test2 here and did not execute, why print out callbak2 callback3, I'll explain # ## timeout4 the Test DEF callback (sec ): the time.sleep (sec) Print ( ' callback4 ' ) return @ timeout4 (the callback, 100) # here is a daemon thread, the end of the line 10s, 100s automatically end, so the main thread is the thread short time DEF Test4 () : the time.sleep ( 10 ) Print ( ' Test4 ' ) test4 ()
Note that the length of time the thread is the guardian.
Here on the decorator simple summary 2:00
1. decorator, a function is modified, all must have the input layer is only a func
2. Decorative first performs a function deco decorative and deco is always a top-level function [to solve the above 'print out callbak2 callback3' problems]
Import Threading DEF timeout2 (sec, the callback): # decorator decorator with parameters DEF _deco (FUNC): T = Threading.Timer (sec, the callback) t.start () # t.join () DEF myfunc (Arg) : FUNC (Arg) return myfunc return _deco DEF callback (): Print ( ' callback2 ' ) @ timeout2 ( 1, callback) # Now Print callback2, and ends DEF test2 (the X-): Print (the X-)
It did not perform any function, while the deco is performed automatically.
Scenario 2: Thread guard simple version
Import Time Import Threading DEF func1 (): the time.sleep ( 10 ) Print ( ' func1 ' ) return . 1 DEF func2 (): the time.sleep ( 20 is ) Print ( ' func2 ' ) return 2 DEF Test (): T2 = Threading .thread (target = func2) # slow thread as a daemon thread t2.setDaemon (True) t2.start () func1 () the Test () # After 10s print func1, When the thread fast, slow thread automatically end
Here you can override the run method of obtaining output func2, extended own demands.