简体   繁体   English

如何在不使用 time.sleep 方法的情况下在 python 中进行倒计时?

[英]How to make countdown in python without using time.sleep method?

I'm new to python. I wonder if there any way to make countdown program in python without using any external library and time.sleep method?我是python的新手,请问有没有不用外部库和time.sleep方法在python中制作倒计时程序的方法? Please give me an example with code.请给我一个代码示例。 thanks in advance.提前致谢。

You could use the Unix Timestamp, which gives you a precise value of the seconds wich have passed since the first day of 1970. By substracting a startvalue of this from the actual time each checking time, you can calculate the time that has passed since your desired time.您可以使用 Unix 时间戳,它为您提供自 1970 年第一天以来经过的秒数的精确值。通过从每次检查时间的实际时间中减去此起始值,您可以计算出自您的时间以来经过的时间所需的时间。 You can get the Unixseconds in python with您可以使用 python 获取 Unixseconds

time.time()

Of course, this would require importing the time module, but this is a builtin library hence why I wouldnt classify it as additional library.当然,这需要导入时间模块,但这是一个内置库,因此我不将其归类为附加库。

Simple answer:简单的答案:
No, it is not directly possible to wait without any imports.不,没有任何导入是不可能直接等待的。 The Python modules do this very efficiently, you should use them. Python 模块非常有效地执行此操作,您应该使用它们。

But there's a dirty option for UNIX/Linux:但是对于 UNIX/Linux 有一个肮脏的选择:
Without any imports you could read the time (and the progressing of time) from /proc/uptime and build a wait function on your own.在没有任何导入的情况下,您可以从/proc/uptime读取时间(以及时间的进度)并自行构建等待 function。

Note that this is not in any way the best practice and very inefficient.请注意,这绝不是最佳实践,而且效率非常低。 But, if you really can't use modules in your program it could look something like this:但是,如果你真的不能在你的程序中使用模块,它可能看起来像这样:

def wait(seconds):
    uptime_start = getUptime()
    uptime = uptime_start
    while(uptime != uptime_start + seconds):
        uptime = getUptime()
    
def getUptime():
    with open("/proc/uptime", "r") as f:
        uptime = f.read().split(" ")[0].strip()
    return int(float(uptime))

print("Hello, let's wait 5 seconds")
wait(5)
print("Time passed quickly.")

This does not work for Windows. There are no alternatives, as none of Python 3.9.6 built-in functions (see https://docs.python.org/3/library/functions.html ) allow for the measuring of time.这不适用于 Windows。没有其他选择,因为 Python 3.9.6 内置函数(请参阅https://docs.python.org/3/library/functions.html )都不允许测量时间。

If you want your Python code to stop at some point without freezing other parts of your program, you won't get around using modules.如果您希望您的 Python 代码在某个点停止而不冻结程序的其他部分,您将无法绕过使用模块。

It would work like this:它会像这样工作:

import threading

def waitForThis():
    print("now the time has passed")

# after 5 seconds, 'waitForThis' gets executed
timer = threading.Timer(5.0, waitForThis)
timer.start()

print("I can write this while it's running")
number = 5
print("Countdown!")
while True:
 print(number)
 number = number - 1
 if number <= 0:
  break

print("Now!")

ez pz z pz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM