gpt4 book ai didi

python - 间隔重复功能?

转载 作者:行者123 更新时间:2023-11-28 17:48:05 25 4
gpt4 key购买 nike

我正在制作一个 wxPython 应用程序,我需要每 15 秒从 Internet 更新一个值。有什么方法可以让我有一个函数来设置值,并让它以这个间隔在后台运行,而不会中断程序?

编辑:这是我正在尝试的:

import thread

class UpdateThread(Thread):
def __init__(self):
self.stopped = False
UpdateThread.__init__(self)
def run(self):
while not self.stopped:
downloadValue()
time.sleep(15)
def downloadValue():
print x

UpdateThread.__init__()

最佳答案

您想要的是添加一个以指定速度运行您的任务的线程。

您可以在这里查看这个很棒的答案:https://stackoverflow.com/a/12435256/667433帮助您实现这一目标。

编辑:这是适合您的代码:

import time
from threading import Thread # This is the right package name

class UpdateThread(Thread):
def __init__(self):
self.stopped = False
Thread.__init__(self) # Call the super construcor (Thread's one)
def run(self):
while not self.stopped:
self.downloadValue()
time.sleep(15)
def downloadValue(self):
print "Hello"

myThread = UpdateThread()
myThread.start()

for i in range(10):
print "MainThread"
time.sleep(2)

希望对你有帮助

关于python - 间隔重复功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15225252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com