如何同时运行计时器和另一个函数,对计时器返回的时间做出反应?[蟒蛇]



我有一个计时器,返回(至少我希望它(一些值。我有一个函数,外部计时器类,它应该查看计时器的返回值,如果该函数接收特定数据 - 仅在该特定时刻执行其他操作。

from time import time, sleep
class Clock:
def __init__(self):
self.oneSec = 0
self.secsPassed = 0 #counts all seconds that already passed, up to 59; then resets and counts again
self.minute = 0
self.hour = 0 #currently unused
self.day = 0 #currently unused
self.start_time = 0
self.start()
def start(self):
while self.secsPassed < 10:
self.start_time = time()
sleep(1)
self.oneSec = int(time() - self.start_time) #easiest way to measure a second
self.secsPassed += self.oneSec
print(f"{self.minute} minutes, {self.secsPassed} seconds")
self.getStats() #returns values without breaking the loop, at least I hope it does 
if self.secsPassed == 10: #normally it'd be 59, but I wanted to do it quicker for sake of tests
self.secsPassed = -1 #with 0 after 1:59 minute there would be immediately 2:01
self.minute += 1
if self.minute == 2 and self.secsPassed == 0: #just to end test if everything works fine
break #for some reason it doesn't totally exit whole code, only stops counting, but I still have to manually exit the code from running

def getStats(self):
return self.day, self.hour, self.minute, self.secsPassed

现在我需要在 Clock 类之外的一个函数,它可以观察我的 Clock 的返回语句如何变化并相应地对它们做出反应。我已经做了一些编码,但它不起作用,我可能知道为什么,但我无法在 3 小时内想出解决方案。这是我当前的代码:

def clockChecker(clock):
while True: #I thought it'd be a good idea to loop since it has to be "conscious" the whole time to react on new returns, but... I guess this cannot work, after a few hours I realized that by running this function it gets the returned statement only once, so it can't update
if clock == (0, 0, 0, 3): #if clocks makes 3rd second
print("It works!")
break

我尝试使用线程、管道和池,但如果我的时钟检查器无效,则无济于事。 首先,我绝对需要有关我的时钟检查器的帮助,其次,我希望至少选择我应该使用哪一个(线程,管道,池(,因此它工作非常顺利。

你的方法会有一些困难。

第一个只是clockChecker功能,我相信你错过了一个.getStats()调用来做这项工作,它没有成功的机会。

即使进行了此更改(应该有效(,完全匹配也是非常危险的,并且会受到"在正确的时间不匹配"和"可能永远不会匹配"的影响。

处理此问题的一种方法是使用匹配超过特定时刻的>=或其他条件。 其他包括:threading.Event;将条件推入计时器的钩子;按原样执行逻辑,休眠三秒钟;协程以及更多:)

打印格式被修改,因为我使用 python 3.5

from time import time, sleep
import threading 
class Clock:
def __init__(self):
self.oneSec = 0
self.secsPassed = 0 #counts all seconds that already passed, up to 59; then resets and counts again
self.minute = 0
self.hour = 0 #currently unused
self.day = 0 #currently unused
self.start_time = 0
#self.start()
def start(self):
while self.secsPassed < 10:
self.start_time = time()
sleep(1)
self.oneSec = int(time() - self.start_time) #easiest way to measure a second
self.secsPassed += self.oneSec
print("%d minutes, %d seconds"%(self.minute,self.secsPassed,))
# self.getStats() #returns values without breaking the loop, at least I hope it does 
if self.secsPassed == 10: #normally it'd be 59, but I wanted to do it quicker for sake of tests
self.secsPassed = -1 #with 0 after 1:59 minute there would be immediately 2:01
self.minute += 1
if self.minute == 2 and self.secsPassed == 0: #just to end test if everything works fine
break #for some reason it doesn't totally exit whole code, only stops counting, but I still have to manually exit the code from running

def getStats(self):
return self.day, self.hour, self.minute, self.secsPassed

def clockChecker(clock):
while True: #I thought it'd be a good idea to loop since it has to be "conscious" the whole time to react on new returns, but... I guess this cannot work, after a few hours I realized that by running this function it gets the returned statement only once, so it can't update
if clock.getStats() == (0, 0, 0, 3): #if clocks makes 3rd second
print("It works!")
break
sleep(1)
clock=Clock()
th1=threading.Thread(target=lambda: clock.start())
th1.start()
th2=threading.Thread(target=clockChecker,args=(clock,))
th2.start()
th2.join()
th1.join()

如果您不介意改用另一条路线

def outside_func():
do something
return
class clock():
def __init__(self, arguments):
self.secspassed
def timer():
while True:
self.secspasswd += 1
if self.secspasswd % 3 == 0:
outside_func()
time.sleep(1)   

我认为使用 +1 秒来设置时间值会减少偏见的增加。 编程+1需要一些时间,设置属性值也需要一些时间。当您+1时,实时时间已超过1秒,

最新更新