我想在每次执行代码的时候执行一段代码,就像播放媒体文件一样……(每次在完全相同的时间内执行相同的代码)
这在python中是可能的吗?
发布于 2011-01-16 03:29:01
这应该能起到作用:
def run_with_delay(funcs, interval):
for f in funcs[:-1]:
before = time()
f()
# compensate the interval with the execution time.
# NB: careful for functions that have a greater
# execution time than interval
after = time()
if after - before < interval:
sleep(interval - (after - before))
# last function is taken separately because we don't need
# an extra useless sleep
funcs[-1]()发布于 2011-01-16 03:18:00
我不认为语言构造(在任何语言中)都能保证这一点--您必须使用实时操作系统。我相信多媒体应用程序利用设备级缓冲来补偿操作系统进程调度程序中的定时抖动。
发布于 2011-01-16 03:17:05
我认为这在一个交错指令来模拟多线程同时执行的操作系统中是不可能的。
你需要一个实时库或语言来规定你的代码的最后期限,即使这样也不能保证在分配的时间内执行。
https://stackoverflow.com/questions/4701576
复制相似问题