python - 在 python 中运行函数 'x' no。仅秒数

标签 python

我想运行一个 python 脚本(或其中定义的函数)一段时间间隔,比如 67 秒。我尝试了以下操作,但程序不会停止运行。

strt = time.time() + 67
print(strt)

with open('csv_data.csv', 'w') as f1:
    writer = csv.writer(f1)

    if z1serial.is_open:
        while True:
            while (time.time() < strt):
                clock.tick(60)
                size = z1serial.inWaiting()
                if size:
                    data = (z1serial.read(size))
                    writer.writerow(data)
                    writer.writerow('',)

最佳答案

因为这里有一个嵌套循环,而外循环是一个无限循环。内循环将在特定时间段后停止,但外循环不会。去掉外层循环

with open('csv_data.csv', 'w') as f1:
    writer = csv.writer(f1)

    if z1serial.is_open:
        while (time.time() < strt):
             clock.tick(60)
             size = z1serial.inWaiting()
             if size:
                 data = (z1serial.read(size))
                 writer.writerow(data)
                 writer.writerow('',)

关于python - 在 python 中运行函数 'x' no。仅秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44492780/

相关文章:

python - %s 设置正确但 PyMySQL 仍然提示 %d

javascript - 在 django 登录和注销事件上运行 javascript

Python3 属性错误 : 'list' object has no attribute 'clear'

python - python 判断两个ip地址是否是同一个网络

python - python中的SVM(支持向量机)总是给出相同的预测

java - 通过 http 调用远程 .bat 文件

python - 如何在按住按键的同时刷新 OpenCV 窗口

python - 位操作给出错误的输出

python - numpy dtype int 类型错误

php - Linux 对 webservice 的编辑权限