The multiprocessing with barrier solution didn't work for me, I blame my python environment. Got a FileNotFoundError from the multiprocessing semlock trying to rebuild the state.
Using the solution from l4mpi that used time() and a while loop to wait for an agreed upon time worked, but was off by a few milliseconds.
I found using the datetime.datetime.now() got me to within a few microseconds. This example same code from l4mpi (modified to work on my python, then swapped time() to datetime.datetime.now() based checking)
from multiprocessing import Process
import datetime
start_time = datetime.datetime.now().second + 10
start_time_micro = 500
def func_1(title):
now = datetime.datetime.now()
print(f"hello, world {title}")
print( "Current second: %d" % now.second)
print("Current microsecond: %d" % now.microsecond)
def func_2(name):
now = datetime.datetime.now()
print(f"Bye, world {name}")
print("Current second: %d" % now.second)
print("Current microsecond: %d" % now.microsecond)
def start_f1(name):
while True:
now = datetime.datetime.now()
if now.second >= start_time and now.microsecond >= start_time_micro:
break
func_1(name)
def start_f2(name):
while True:
now = datetime.datetime.now()
if now.second >= start_time and now.microsecond >= start_time_micro:
break
func_2(name)
if __name__ == '__main__':
procs = []
print(f"setup: {start_time}")
procs.append(Process(target=start_f2, args=('bob',), daemon=True))
procs.append(Process(target=start_f1, args=('sir',), daemon=True))
start_time = datetime.datetime.now().second + 10
print(f"Starting {start_time}")
for x in procs:
x.start()
for x in procs:
x.join()
print("Done")
Don't know why the
map(lambda x: x.start(), procs)
map(lambda x: x.join(), procs)
didn't work in my system, just converted to a for loop to avoid wasting time.
All in all it looked weird that one time function was used to print and check the difference between the two, while another time function was used to synchronize the two. So I wanted to test it out. Then the results were just interesting enough that I wanted to share with everyone else. Hope someone will find this helpful or informative.