I want to execute the following two functions at exactly the same time.
from multiprocessing import Process
import os
import datetime
def func_1(title):
now = datetime.datetime.now()
print "hello, world"
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
def func_2(name):
func_1('function func_2')
now = datetime.datetime.now()
print "Bye, world"
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
if __name__ == '__main__':
p = Process(target=func_2, args=('bob',))
p.start()
p.join()
And I am getting a difference in microseconds. Is there any way to execute both at the exact same time? Any help would be appreciated.
Just like we execute Thread s using the start () and join () functions, we also execute Process with start () and join () APIs. An important aspect of using the multiprocessing library in Python is that you have to run the processes under if __name__ == “__main__”.
One option, that looks like it makes two functions run at the same time, is using the threading module (example in this answer). However, it has a small delay, as an Official Python Documentation
Similar to multithreading, multiprocessing in Python also supports locks. We can set the lock to prevent the interference of threads. When the lock is set, a process starts only when the previous process is finished and the lock is released. We can do this by importing the Lock object from the multiprocessing module.
However, some functions can still virtually run in parallel. Python allows this with two different concepts: multithreading and multiprocessing. In this post we’ll go over how to run two functions virtually in parallel with multiprocessing. Multiprocessing is a native Python library that supports process based parallelism.
On the computer the following was written on, this code consistently prints out the same timestamps:
#! /usr/bin/env python3
from multiprocessing import Barrier, Lock, Process
from time import time
from datetime import datetime
def main():
synchronizer = Barrier(2)
serializer = Lock()
Process(target=test, args=(synchronizer, serializer)).start()
Process(target=test, args=(synchronizer, serializer)).start()
def test(synchronizer, serializer):
synchronizer.wait()
now = time()
with serializer:
print(datetime.fromtimestamp(now))
if __name__ == '__main__':
main()
This is (1) generally impossible (the "exact" part) and (2) not something that Python is good at. If you really need microsecond execution precision, use C or ASM, but an even closer way than COpython's answer would be busy-waiting in two different processes for an agreed start time:
from multiprocessing import Process
import os
import datetime
from time import time
def func_1(title):
now = datetime.datetime.now()
print "hello, world"
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
def func_2(name):
now = datetime.datetime.now()
print "Bye, world"
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
def start_f1(name):
while time() < start_time: pass
func_1(name)
def start_f2(name):
while time() < start_time: pass
func_2(name)
if __name__ == '__main__':
procs = []
procs.append(Process(target=start_f2, args=('bob',)))
procs.append(Process(target=start_f1, args=('sir',)))
start_time = time() + 10
map(lambda x: x.start(), procs)
map(lambda x: x.join(), procs)
I am not sure if this will execute at exactly the same time, but I think that it will get you closer.
from multiprocessing import Process
import os
import datetime
def func_1(title):
now = datetime.datetime.now()
print "hello, world"
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
def func_2(name):
now = datetime.datetime.now()
print "Bye, world"
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
if __name__ == '__main__':
procs = []
procs.append(Process(target=func_2, args=('bob',)))
procs.append(Process(target=func_1, args=('sir',)))
map(lambda x: x.start(), procs)
map(lambda x: x.join(), procs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With