Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python thread timer Timer

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use Timer thread timer". The content is easy to understand and clear, hoping to help you solve your doubts. Let me lead you to study and learn this article "how to use Python thread timer Timer".

one。 Thread timer Timer principle

The principle is relatively simple, start the thread after the specified time interval! Applicable scenarios: complete scheduled tasks, such as: scheduled reminder-alarm clock and so on.

one

two

three

four

# Import Thread Module

Import threading

Timer = threading.Timer (interval, function, args=None, kwargs=None)

Parameter description:

Interval-timer interval, how many seconds after which the timer task is started (in seconds)

Function-Thread function

Args-Thread parameter, which can pass tuple type data. Default is empty (default parameter)

Kwargs-Thread parameter, which can pass dictionary type data. Default is empty (default parameter)

two。 Thread timer use

Usage scenario: timed alarm clock

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

#! usr/bin/env python

#-*-coding:utf-8 _ *-

"

@ Author: how to solve the problem

Blog (personal blog address): shuopython.com

@ WeChat Official Account (official Wechat account): ape says python

@ Github:www.github.com

@ File:python_timer.py

@ Time:2019/10/22 21:25

@ Motto: if you don't accumulate steps, you can't reach thousands of miles. If you don't accumulate small streams, you can't become rivers and seas. The splendor of program life needs to be accumulated unremittingly.

"

# Import Thread Module

Import threading

Def thread_Timer ():

Print ("it's time to get up. Wake you up again in 5 seconds.")

# declare global variables

Global t1

# create and initialize threads

T1 = threading.Timer (5 threadbare timer)

# start thread

T1.start ()

If _ name__ = = "_ _ main__":

# create and initialize threads

T1 = threading.Timer (5, thread_Timer)

# start thread

T1.start ()

Output result:

one

two

three

four

five

six

seven

eight

nine

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

It's time to get up. I'll call you up again in 5 seconds.

Code analysis:

The thread timer is created in the main thread, and the thread_ timer thread function is executed after 5 seconds, and at the end of the thread_Timer function, the timer thread thread_Timer is set, which completes a recursive operation and repeats the timing task with an interval of 5 seconds!

three。 Key summary

Thread and thread interaction in many ways, so far has been explained thread mutex lock Lock, thread event Event, thread condition variable Condition, thread timer Timer, different requirements, the use of different ways, flexible use!

The above is all the content of the article "how to use Python thread timer Timer". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report