directory
- Python thread Timer Principle
- The Python thread Timer is used
- Python thread Timer Summary
- Four. Guess you like it
Recommended path for learning Python: Python Learning Directory >> Python Basics
Compared to the previous Python threads articles, this one is relatively simple. Timers — as the name implies, are used for timed tasks.
Python thread Timer Principle
The principle is relatively simple, after the specified time interval to start the thread! Application scenario: Perform scheduled tasks, for example, perform scheduled reminders and alarm clocks.
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python thread Timer timer. py @time :2021/05/04 07:37 @Motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! Interval - The number of seconds after the timer task is started (in seconds); Function - thread functions; Args - thread arguments that can pass tuple type data and default to null (default argument); Kwargs - thread argument, which can pass dictionary type data, is null by default (default argument); ''' timer = threading.Timer(interval, function, args=None, kwargs=None)Copy the code
The Python thread Timer is used
Usage scenario: A scheduled alarm clock
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python thread Timer timer. py @time :2021/05/04 07:37 @Motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! Import threading def thread_Timer(): print(" thread_thread_timer "): print(" thread_thread_timer "): print(" thread_thread_timer "): print(" thread_thread_timer ") Wake you up again 5 seconds later..." T1 = threading.timer (5,thread_Timer) # start thread t1.start() if __name__ == "__main__": T1 = threading.timer (5, thread_Timer) t1 = threading.timer (5, thread_Timer) Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... It's time to get up... Wake you up again 5 seconds later... ' ' 'Copy the code
Code analysis:
Create a thread timer on the main thread, execute the thread_Timer function 5 seconds later, and set the timer thread thread_Timer at the end of the thread_Timer function. This completes a recursive operation, repeating the timer task 5 seconds apart!
Python thread Timer Summary
Python threads interact with threads in many ways. So far, we have explained the thread mutex Lock, thread Event, thread Condition variable, and thread Timer.
Four.Guess you like
- Python conditional derivations
- Python list derivations
- Python dictionary derivations
- Python function declarations and calls
- Python variable argument *argc/**kargcs
- Python anonymous function lambda
- Python return logic determines expressions
- Python string/list/tuple/dictionary conversions
- Python local and global variables
- The Python type function is different from the isinstance function
- Python is differs from ==
- Python mutable and immutable data types
- Shallow and deep copies of Python
- Read and write Python files
- Python exception Handling
- Python module import
- Python __name__ == ‘__main__’ explained in detail
Python thread Timer Timer
This article is published by the blog – Ape Say Programming Ape Say programming!