python thread timer Timer (32)

   

    Relative front of several python thread content, the content of the film is relatively simple, timer - as the name implies, necessarily for regular tasks.

Guard against shock

A Thread Timer Timer principle

    Principle is simple, after a specified time interval start thread! Applicable scene: the completion of regular tasks, such as: timed reminder - alarm clock and so on.

# Import the thread module 
Import Threading
 
timer = threading.Timer(interval, function, args=None, kwargs=None)

 

Parameter Description:

    interval The  - interval timer, task timer is started after an interval of how many seconds (unit: seconds);

    function  - thread function;

    args  - thread parameters can pass data tuple type, default is empty (default parameters);

    kwargs  - thread parameters, data dictionaries can pass, default is empty (default parameters);

 

 

II. Use a timer thread

    Usage scenarios: Alarm,

    Output:

Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...
Get out of bed ... 5 seconds after the call again you get up ...

 

    Code analysis:

    In the main thread creates a thread timer, perform thread_Timer thread function after 5 seconds, and when thread_Timer end of the function, but also set a timer thread thread_Timer, which completed a recursive operation, repeat the timing interval of 5 seconds task!

 

III. Key summary

    There are many ways to interact with the thread of the thread so far has explained the thread mutex Lock , thread the event Event , thread condition variable for condition Condition , thread timer Timer, different needs, use may be different, flexible use!

 

you may also like:

    1.python thread events for Event

    2.python thread condition variable Condition

    3.python thread mutex Lock

    4.python thread creation and parameter passing

 

    Reproduced please specify: ape say Python  »  Python thread timer Timer

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/11957735.html