delay.py 924 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @文件 :delay.py
  5. @时间 :2022/02/17 15:37:43
  6. @作者 :None
  7. @版本 :1.0
  8. @说明 :精确到ms延迟函数
  9. '''
  10. import sys
  11. import ctypes
  12. from time import sleep
  13. def m_delay(ms):
  14. if sys.platform == "darwin" or sys.platform == "linux":
  15. sleep(ms / 1000)
  16. elif sys.platform == "win32" or sys.platform == "win64":
  17. stop_value = ctypes.c_longlong(0)
  18. start_value = ctypes.c_longlong(0)
  19. freq = ctypes.c_longlong(0)
  20. n = 0
  21. ctypes.windll.kernel32.QueryPerformanceFrequency(ctypes.byref(freq))
  22. count = ms * freq.value / 1000
  23. ctypes.windll.kernel32.QueryPerformanceCounter(ctypes.byref(start_value))
  24. while n < count:
  25. ctypes.windll.kernel32.QueryPerformanceCounter(ctypes.byref(stop_value))
  26. n = stop_value.value - start_value.value
  27. else:
  28. sleep(ms / 1000)