목록에서 빠르고 정확하게 메시지를 보내야합니다. 하나의 목록은 +/- 10ms 간격으로 100ms마다 메시지를 보내야합니다. 나는 아래의 코드를 사용해 보았지만, 문제는 타이머가 100ms를 기다린 다음 모든 계산을 완료해야 타이머가 허용되는 윈도우에서 떨어지는 것이다.빠르고 정확한 Python 반복 타이머
단순히 대기 시간을 줄이면 지저분하고 신뢰할 수없는 해킹입니다. 루프 중에 목록이 편집되는 경우 메시지 루프 주위에 Lock이 있습니다.
python이 메시지를 100ms 내내 일관되게 보내는 방법에 대한 생각은 무엇입니까? 감사합니다
from threading import Timer
from threading import Lock
class RepeatingTimer(object):
def __init__(self,interval, function, *args, **kwargs):
super(RepeatingTimer, self).__init__()
self.args = args
self.kwargs = kwargs
self.function = function
self.interval = interval
self.start()
def start(self):
self.callback()
def stop(self):
self.interval = False
def callback(self):
if self.interval:
self.function(*self.args, **self.kwargs)
Timer(self.interval, self.callback,).start()
def loop(messageList):
listLock.acquire()
for m in messageList:
writeFunction(m)
listLock.release()
MESSAGE_LIST = [] #Imagine this is populated with the messages
listLock = Lock()
rt = RepeatingTimer(0.1,loop,MESSAGE_LIST)
#Do other stuff after this
나는 writeFunction이 약간의 지연을 일으키지 만 10ms는 넘지 않는다는 것을 이해합니다. 필자는 본질적으로 각 메시지마다 100ms마다 함수를 호출해야합니다. messagelist는 작고, 보통 요소보다 작습니다. P
'10ms마다와 작업, +/- 1ms의'하나 :( –