Python: high precision time.sleep

前端 未结 3 1451
小蘑菇
小蘑菇 2020-12-06 04:04

can you tell me how can I get a high precision sleep-function in Python2.6 on Win32 and on Linux?

相关标签:
3条回答
  • 2020-12-06 04:33

    You can use floating-point numbers in sleep():

    The argument may be a floating point number to indicate a more precise sleep time.

    So

    time.sleep(0.5)
    

    will sleep for half a second.

    In practice, however, it's unlikely that you will get much more than millisecond precision with sleep because operating systems usually only support millisecond sleeps and because very short amounts of time quickly get unreliable.

    0 讨论(0)
  • 2020-12-06 04:43
    def start(self):
        sec_arg = 10.0
        cptr = 0
        time_start = time.time()
        time_init = time.time()
        while True:
            cptr += 1
            time_start = time.time()
            time.sleep(((time_init + (sec_arg * cptr)) - time_start ))
    
            # AND YOUR CODE .......
            t00 = threading.Thread(name='thread_request', target=self.send_request, args=([]))
            t00.start()
    

    It's a very good precision on linux

    0 讨论(0)
  • 2020-12-06 04:57

    Here is a similar question:

    how-accurate-is-pythons-time-sleep

    On linux if you need high accuracy you might want to look into using ctypes to call nanosleep() or clock_nanosleep(). I'm not sure of all the implications of trying that though.

    0 讨论(0)
提交回复
热议问题