python - Python“for循环”循环时间限制
问题描述
如何为“for循环”设置时间限制?
说我想要每 200 毫米循环一次
for data in online_database:
looping time = 200 mms
print(data)
谢谢!
解决方案
也许这样的事情可以帮助
import time
for i in YourSequence:
current_millis = round(time.monotonic() * 1000)
max_milis = (current_millis + 200) #200 is for the time difference
while round(time.monotonic() * 1000) < max_milis:
#----Your code------
此外,如果您的程序很大,您可以添加这些行以定期留意您的程序是否超过了最大时间限制
if round(time.monotonic() * 1000) > max_milis:
break
像这样的东西
import time
for i in a:
current_millis = round(time.monotonic() * 1000)
max_milis = (current_millis + 200)
while round(time.monotonic() * 1000) < max_milis:
#Your Codes......
if round(time.monotonic() * 1000) > max_milis:
break
#Your Codes......
if round(time.monotonic() * 1000) > max_milis:
break
#Your Codes......
break
推荐阅读
- node.js - 尝试发送 json 对象时,fetch api 如何与 express 一起使用
- ruby-on-rails - RoR API 如何使用帖子进行关联
- swift - iOS CallKit:CXProviderConfiguration localizeName 已弃用
- python - 寻找修复算法的方法。使用动态规划实现组合和 II
- macos - 运行 cron 作业时执行 Fastlane 命令缺少 Gem
- android - 使用 Handler 删除 Firebase DatabaseReference 侦听器的替代方法是什么?
- javascript - ES6提取具有id数组的对象的数组过滤数组
- apache-kafka - Kafka - 在错误主题中重新驱动事件
- python - 如何循环和附加 pd 列 + groupby 逻辑
- algorithm - 用于生成基于网格的城镇的程序算法的想法