DEV Community

ComputerSmiths
ComputerSmiths

Posted on

Synchronize program start over network

I'm trying to get two computers to start a program (to play a video) simultaneously using a network connection in Python.

I'm starting down the path of opening a socket server on one machine, connecting to it on the other, and sending some kind of communication saying "Now!", which is do-able, but seems unnecessarily complicated.

Is there some easy thing I'm missing here? Ideally I'd have a program called "Block.py" that I'd start from a bash script on one machine, which would wait till I run it on the other machine, and they'd then both continue on to the omxplayer command...

Both machines have static IPs, if that helps simplify things.

Many Thanks in advance!

Top comments (3)

Collapse
 
madhadron profile image
Fred Ross

You're going to open a socket to somewhere. There has to be some means of communication. If it's a throwaway and you don't need synchronization better than a few hundred milliseconds, and it's only these two machines, call one of them the master, one of them the minion, and write a pair of bash scripts.

On the master, the bash script runs ncat or netcat listening for a connection a port. As soon as it receives the connection and netcat exits, run omxplayer.

On the minion, use ncat or netcat to connect to the master, and once it exits, run omxplayer.

This is brittle, it can't give you better precision, if there are errors it has to be reset by hand, but if you really just need this to happen, it will work.

If it is likely to grow to other machines or you need better precision then you need to do something more sophisticated. One of my usual tricks for an architecture is to replace any fixed number of things with an arbitrary number that might be very large and think about how to make it work. If I needed higher precision and many machines synchronized, I would use something like Zookeeper or etcd (hereafter the coordinator). Make sure you have NTP synchronizing the time to the necessary precision (or add atomic clocks to the machine if you need really high precision). Each of the minions connects to coordinator and elects a master, starts the video paused, then waits on a shared location in the coordinator to have a value set. The master picks a timestamp a little bit in future and sets it into that location. Each machine is notified of the new value, loops until it reaches that timestamp, and starts the video.

Collapse
 
computersmiths profile image
ComputerSmiths

Ah, ncat would have worked, thanks! Shouldn't need any more than the two machines, it's a couple of Raspberry Pis playing videos on a pair of "Lady Gaga Glasses", so unless she grows a third eye, I should be good. 8*)

Thanks again!

Collapse
 
computersmiths profile image
ComputerSmiths

Turns out it wasn't as hard as I thought, the example code at realpython.com/python-sockets/ has a server and client that do pretty much what I want. [Hint, gotta use 'python3' instead of 'python' but since I'm calling it from bash, that's a don't care...