如何让我的程序休眠 50 毫秒?
如何让我的 Python 程序休眠 50 毫秒?
How do I get my Python program to sleep for 50 milliseconds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何让我的 Python 程序休眠 50 毫秒?
How do I get my Python program to sleep for 50 milliseconds?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
使用
time.sleep()
Use
time.sleep()
请注意,如果您依赖睡眠恰好 50 毫秒,您将无法得到这一结果。 这只是关于它。
Note that if you rely on sleep taking exactly 50 ms, you won't get that. It will just be about it.
使用
time.sleep()
:请参阅 Python 文档: https://docs.python.org/library/time.html#time.sleep
Use
time.sleep()
:See the Python documentation: https://docs.python.org/library/time.html#time.sleep
有一个名为“时间”的模块可以帮助您。 我知道两种方法:
睡觉
Sleep(参考)要求程序等待,并且然后执行其余的代码。
有两种使用睡眠的方法:
第二种方式不导入整个模块,只是休眠。
使用
time.monotonic()
使用启动后的时间。如果您需要运行循环,这种方法很有用。 但这一个稍微复杂一些。
time.monotonic
比time.time
更好,因为它不考虑闰秒,但它会计算启动时的设置数量。 (图片来源:马克·拉卡塔)There is a module called 'time' which can help you. I know two ways:
sleep
Sleep (reference) asks the program to wait, and then to do the rest of the code.
There are two ways to use sleep:
The second way doesn't import the whole module, but it just sleep.
Using time since boot using
time.monotonic()
.This way is useful if you need a loop to be running. But this one is slightly more complex.
time.monotonic
is better thantime.time
as it does not account for leap seconds, but it counts the amount of settings from boot. (Credit: Mark Lakata)您还可以使用
Timer()
函数来完成此操作。代码:
You can also do it by using the
Timer()
function.Code:
您还可以将 pyautogui 用作:
如果第一个参数不是 None,那么它将暂停第一个参数的秒数,在本例中: 0.05 秒
如果第一个参数是 None,第二个参数是 True,那么它将休眠全局暂停设置,设置为:
如果您想知道原因,请参阅源代码:
You can also use pyautogui as:
If the first argument is not None, then it will pause for first argument's seconds, in this example: 0.05 seconds
If the first argument is None, and the second argument is True, then it will sleep for the global pause setting which is set with:
If you are wondering about the reason, see the source code: