在python中是否有类似Arduino的millis()的功能?

问题描述 投票:0回答:3

Python中是否有类似Arduino的millis()的函数(不是delay()函数)?我想暂时停止程序的特定部分,而不是整个程序。

python time
3个回答
1
投票

尝试使用utime功能

https://docs.micropython.org/en/latest/library/utime.html

希望有所帮助。


0
投票

您尝试使用

import time
seconds_to_sleep=1
time.sleep(seconds_to_sleep)

0
投票

这个问题有点不清楚,因为millis()只是给您程序开始执行以来的时间,但是您想暂停程序的某些部分。

Python脚本作为单线程进程运行,如果您想阻止程序的特定部分而不阻止其余部分,则首先需要启动其他线程和/或进程,并在它们之间拆分逻辑。] >

https://realpython.com/intro-to-python-threading/

[AFAIK没有内置的“自启动以来的时间”变量(但是我可能错了,当然,乐于学习),我会在您的time.time()块中添加一个__main__调用:

import time


def main():
    global time_of_startup

    # ... lots of code here ... 

    time_since_start = time.time() - time_of_startup


if __name__ == "__main__":
    time_of_startup = time.time()
    main()
© www.soinside.com 2019 - 2024. All rights reserved.