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

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python function declarations and calls
  5. Python variable argument *argc/**kargcs
  6. Python anonymous function lambda
  7. Python return logic determines expressions
  8. Python string/list/tuple/dictionary conversions
  9. Python local and global variables
  10. The Python type function is different from the isinstance function
  11. Python is differs from ==
  12. Python mutable and immutable data types
  13. Shallow and deep copies of Python
  14. Read and write Python files
  15. Python exception Handling
  16. Python module import
  17. Python __name__ == ‘__main__’ explained in detail

Python thread Timer Timer

This article is published by the blog – Ape Say Programming Ape Say programming!