[英]How can I simultaneously run a timer and another function, reacting to what time does the timer return? [Python]
我有一個計時器,返回(至少我希望它)一些價值。 我有一個函數,在定時器類之外,它應該查看定時器的返回值,如果該函數接收到特定數據 - 只在特定時刻執行其他操作。
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 無效,則沒有任何幫助。 首先,我肯定需要我的clockChecker 的幫助,其次我很感激至少在選擇我應該使用哪個(線程、管道、池)方面的幫助,所以它工作得非常順利。
你會發現你的方法有一些困難。
第一個只是clockChecker
函數,我相信您缺少.getStats()
調用來使其工作,它沒有成功的機會。
即使進行了這種更改(應該起作用),精確匹配也非常危險,並且會遭受“在正確的時間不匹配”和“可能永遠不會匹配”的困擾。
解決此問題的一種方法是使用>=
或其他匹配特定時刻以上的條件。 其他包括: threading.Event
; 將條件推入計時器的鈎子; 按原樣執行您的邏輯,睡眠三秒鍾; 協程,以及更多:)
如果你不介意換另一條路線
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 秒,
打印格式被修改,因為我使用的是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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.