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

标签 python function python-2.7 python-3.x time

我想让我的函数运行一段特定的时间,比如 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/

相关文章:

python - 波浪号登录 Pandas 数据框

python - 在枚举列表中过滤枚举 sqlalchemy

python - 使用memory_profiler在python中进行内存管理

python - 使用 PIL 模块的 UnicodeDecodeError

python - 为什么这不起作用?这是 apscheduler 错误吗?

function - 如何在运行时将函数导入到 QTP 测试中?

c - 当函数的返回类型不允许我这样做时,如何在 C 中返回错误代码?

javascript - 理解 Eloquent JavaScript 函数式编程中的 apply 方法示例

regex - 仅否定正则表达式中的特定字符串

python - tkinter.mainloop 不起作用