gpt4 book ai didi

python - 如何使函数在特定时间段内运行?

转载 作者:行者123 更新时间:2023-11-28 18:42:40 24 4
gpt4 key购买 nike

我想让我的函数运行一段特定的时间,比如 5 秒;我该怎么做?

喜欢,

def my_function():
while(time == 10 seconds):
......... #run this for 10 seconds
def my_next_function():
while(time == 5 seconds):
......... #run this for 5 seconds

最佳答案

这一定会对你有帮助。

import time

def myfunc():
now=time.time()
timer = 0
while timer != 10:
end = time.time()
timer = round(end-now)

def mynextfunc():
now=time.time()
timer = 0
while timer != 5:
end = time.time()
timer = round(end-now)


myfunc()
print "myfunc() exited after 10 seconds"

mynextfunc()
print "mynextfunc() exited after 5 seconds"

关于python - 如何使函数在特定时间段内运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24323711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com