Fast and Precise Python Repeating Timer

后端 未结 3 841
难免孤独
难免孤独 2021-01-12 14:39

I need to send repeating messages from a list quickly and precisely. One list needs to send the messages every 100ms, with a +/- 10ms window. I tried using the code below,

3条回答
  •  逝去的感伤
    2021-01-12 15:21

    try this:

    #!/usr/bin/python
    import time;  # This is required to include time module.
    from threading import Timer
    
    def hello(start, interval, count):
        ticks = time.time()
        t = Timer(interval - (ticks-start-count*interval), hello, [start, interval, count+1])
        t.start()
        print "Number of ticks since 12:00am, January 1, 1970:", ticks, " #", count 
    
    dt = 1.25 # interval in sec
    t = Timer(dt, hello, [round(time.time()), dt, 0]) # start over at full second, round only for testing here
    t.start()
    

提交回复
热议问题