How to synchronise events to system clock in python? -


i writing application output fake msf signal raspberry pi computer sync radio clock ntp server. application being written in python , have control on pin plan use outputting signal need synchronise python script system clock. have found how sleep relatively accurate periods haven't found allow me trigger function @ specified system time (top of next minute instance) reasonable degree of accuracy (within 100ms or so)

as asynchronous call (independent program in rest of time, using asyncio's abstracteventloop.call_at syncs system clock. if desired precision depends on system clock, on machine running under linux, has precision within milliseconds (i haven't tested on raspberry pi, though). simple python 3 example this:

import asyncio import datetime import time  def callback(loop):     print('hello world!')     loop.stop() # added make program terminate after demo  event_loop = asyncio.get_event_loop() execution_time = datetime.datetime(2017,8,16,13,37).timestamp()  # adjust system time 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() 

the example supposed write 'hello, world!' on 16th of august, 2017, @ 13:37 utc. notice time within event loop not system time, need express desired execution time in event loop time. have program not stop after execution of task, remove loop.stop() @ end of callback.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -