python-3.x - How do I use Python to implement a circular task that executes every 100ms
问题描述
I needed to simulate two scenarios in which a piece of data would be inserted into the Redis database every 100ms
using redis.xadd("stream_name", {"key": "value"}, maxlen=100, approximate=False)
which is the function of the Redis-Stream
data type. I want to keep the insertion timestamp
of the Redis database the same in both scenarios
Therefore, I also need to set the same start time
, so that at the same start time and the same loop interval
which is 100ms
, the insertion time which is used xadd function of two Redis data should be consistent.
I looked up and found that the Apscheduler
was a good timing task, but its interval was only up to seconds
, not milliseconds
.
I don't know what better way to implement my scenario requirements.
I would appreciate it if you could tell me how to solve it?
解决方案
不确定这是否会满足您的要求,但该time.sleep()
函数可以通过十进制参数执行毫秒。
from time import sleep
while True:
sleep(0.1) # or 100/1000
# do something
推荐阅读
- laravel - Laravel 侧边栏的 View Composer
- angular - Google Firestore 如何在后台工作?
- mysql - Mysql 中的 RowNum,分组依据(MySQL 5.x)
- python - 无法导入模块“lambda_function”:无法从“ctypes”导入名称“WinDLL”(/var/lang/lib/python3.7/ctypes/__init__.py
- python - Google Speech API 在 Python 子进程中不起作用
- c++ - 使用 Bazel 与库之间的循环依赖关系并改变这些库的 strip_include_prefix 属性
- encryption - 使用密码加密/解密数据,而数据库管理员无法解密
- python - 如何用空的内置列表填充 numpy 数组?
- node.js - discord.js/node/mysql:如何等到查询返回结果
- mysql - Mariadb 不会将 CHARACTER_SET_RESULTS 从 latin1 更改为 utf8