+
95
-

回答

两种方式

1、自己写一个函数

#!/usr/local/python3/bin/python3
import time

def timed_execution(interval, function, *args, **kwargs):
while True:
function(*args, **kwargs)
time.sleep(interval)

def say_hello():
print("Hello World!/n")

timed_execution(5, say_hello)

2、使用schedule库

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
import schedule
import time

def job():
print("I'm running..")

# 每隔10秒钟执行一次job函数
schedule.every(10).seconds.do
#schedule.every().hour.at("10:00").do(job)#每天10点执行

while True:
schedule.run_pending()

time.sleep(1)


网友回复

我知道答案,我要回答