我正在写一个应用程序来输出一个假的MSF signal从树莓派一样的计算机,以同步与NTP服务器的无线电时钟。该应用程序是用python编写的,我可以控制我计划用于输出信号的引脚,但我需要将python脚本与系统时钟同步。我已经找到了如何在相对准确的时间内睡眠,但我还没有找到任何可以让我在指定的系统时间(例如,下一分钟的顶部)以合理的精度(在100mS左右)触发函数的方法。
发布于 2017-08-15 22:11:45
由于这是一个异步调用(独立于程序在其余时间所做的事情,我会使用asyncio的AbstractEventLoop.call_at来完成它与系统时钟的同步。如果你获得所需的精度取决于系统时钟,但在我的运行在Linux下的机器上,它的精度通常在几毫秒以内(尽管我还没有在Raspberry Pi上测试过)。
import asyncio
import datetime
import time
def callback(loop):
print('Hello World!')
loop.stop() # Added to make program terminate after demo
event_loop = asyncio.get_event_loop()
execution_time = datetime.datetime(2017,8,16,13,37).timestamp()
# Adjust system time to loop clock
execution_time_loop = execution_time - (time.time() - event_loop.time())
# Register callback
event_loop.call_at(execution_time_loop, callback, event_loop)
try:
print('Entering event loop')
event_loop.run_forever()
finally:
print('Closing event loop')
event_loop.close()这个例子应该写成'Hello,World!‘2017年8月16日,UTC时间13:37请注意,事件循环中的时间不是系统时间,因此您需要用事件循环时间来表示所需的执行时间。要使程序在执行任务后不停止,请删除回调结尾处的loop.stop()。
https://stackoverflow.com/questions/45694187
复制相似问题