Fast and Precise Python Repeating Timer

后端 未结 3 842
难免孤独
难免孤独 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:08

    Yes, the simple waiting is messy and there are better alternatives.

    First off, you need a high-precision timer in Python. There are a few alternatives and depending on your OS, you might want to choose the most accurate one.

    Second, you must be aware of the basics preemptive multitasking and understand that there is no high-precision sleep function, and that its actual resolution will differ from OS to OS too. For example, if we're talking Windows, the minimal sleep interval might be around 10-13 ms.

    And third, remember that it's always possible to wait for a very accurate interval of time (assuming you have a high-resolution timer), but with a trade-off of high CPU load. The technique is called busy waiting:

    while(True):
        if time.clock() == something:
             break
    

    So, the actual solution is to create a hybrid timer. It will use the regular sleep function to wait the main bulk of the interval, and then it'll start probing the high-precision timer in the loop, while doing the sleep(0) trick. Sleep(0) will (depending on the platform) wait the least possible amount of time, releasing the rest of the remaining time slice to other processes and switching the CPU context. Here is a relevant discussion.

    The idea is thoroughly described in the Ryan Geiss's Timing in Win32 article. It's in C and for Windows API, but the basic principles apply here as well.

    0 讨论(0)
  • 2021-01-12 15:10

    Store the start time. Send the message. Get the end time. Calculate timeTaken=end-start. Convert to FP seconds. Sleep(0.1-timeTaken). Loop back.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题