开发者

Precise Event Synchronization with Python

I have a multinode network of VMs spread across several machines, one management VM and 7-15 worker VMs. I would like to run an NTP server on the management VM and have all the worker VMs synchronize with the mangament VM.

From there I plan on having a python process on each worker VM spawn a new thread at at arbitary times from one start date. Each process will know ahead of time (ie when the process starts) when it needs to spawn a new thread.

An approach I can think of is to synchronize the machines with NTP, subtract the known waiting time from the current time and then call usleep on the difference.

Pseudo-Code

for ii in eventTimes:
   curTime = getCurrentTime()
   waitTime = ii - curTime
   usleep(w开发者_StackOverflowaitTime)
   spawnEventThread()

There's no shared data between the threads. The minimum time inbetween each thread being spawned is 1 second and they should be simple enough to finish whatever they're doing in that second.

Any help is appreciated


If you can run an NTP server on the management VM, you can hit that NTP server with pure python code to get the "master" time, and then use that as a base for offset calculations.

>>> import ntplib,datetime
>>> x = ntplib.NTPClient()
>>> datetime.datetime.utcfromtimestamp(x.request('master server').tx_time)
datetime.datetime(2011, 9, 19, 17, 59, 24, 679769)

If you change the getCurrentTime() call in your example to a function that gets the network time, you should be good to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜