让 Python 程序等待

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15472707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:13:39  来源:igfitidea点击:

Make Python Program Wait

python

提问by richzilla

I need to make my python program wait for 200ms before polling for an input of some description. In C# for example, I could use Thread.Sleep()to achieve this. What is the simplest means of doing this in python?

在轮询某些描述的输入之前,我需要让我的 python 程序等待 200 毫秒。例如,在 C# 中,我可以使用它Thread.Sleep()来实现这一点。在 python 中执行此操作的最简单方法是什么?

回答by cnicutar

If you simply want to sleep you can try:

如果你只是想睡觉,你可以尝试:

import time

time.sleep(0.2)

回答by Thanakron Tandavas

Use Timemodule.

使用时间模块。

For example, to delay 1 second :

例如,延迟 1 秒:

import time
time.sleep(1) # delay for 1 seconds

In your case, if you want to get 200 ms, use this instead:

在您的情况下,如果您想获得 200 毫秒,请改用:

time.sleep(0.2)

time.sleep also works with float.

time.sleep 也适用于浮动。

回答by Zhong Xiaoqin

You can use the method sleep()in module time.

您可以使用sleep()module 中的方法time

First you must import module timein your program. After that, you can call the sleep()function.

首先,您必须time在程序中导入模块。之后,您可以调用该sleep()函数。

Add this to your code:

将此添加到您的代码中:

import time
time.sleep(0.2)

回答by Matthew Miles

Use the time libraryand use the command time.sleep()to make it wait. It's more efficient when choosing to extract it from the time libraryand then use just sleep() For Example:

使用时间库并使用命令time.sleep()使其等待。选择从时间库中提取它然后只使用 sleep()时效率更高,例如:

import time
print('hi')
time.sleep(0.2)
print('hello')

Improved:

改进:

from time import sleep
print('Loading...')
sleep(2)
print('Done!')

Note: it is measured in seconds not ms.

注意:它以秒为单位而不是毫秒。