如何实现每秒钟打印一次邮件?

2024-06-16 13:49:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想每隔\uuuuu秒调用一次printFunction方法,但我想在一定次数的调用之后停止调度。例如,每5秒打印10次。从另一个文件中提取这些频率和运行时值。你知道吗

穿着紧身衣跑步。你知道吗

导入计划 导入时间

课程示例:

# basic function to print
def printFunction(self):
    print("Hello world")

def repeated(self):
    # reads in two values from another file
    systemFrequency = float(freqSettings.systemFrequency)
    systemRunTime = int(freqSettings.systemRunTime)
    global count 
    count = 0

    while (count < systemRunTime): 
        self.printFunction
        self.increaseCount


 def increaseCount(self):
    global count
    count += 1

Tags: 文件方法selfdefcount调度次数global
1条回答
网友
1楼 · 发布于 2024-06-16 13:49:49

调度程序运行的函数需要返回CancelJob以停止调度。你知道吗

我是这样实现的:

import schedule

def my_function():
    print("hello world")

def wrapper():
    wrapper.counter +=1
    my_function()
    if wrapper.counter == 10:
        return schedule.CancelJob
wrapper.counter = 0

schedule.every(1).seconds.do(wrapper)

while True:
    schedule.run_pending()
    print(schedule)

显示10个“你好世界”

相关问题 更多 >