Multiprocessing in Python: execute two functions at the same time

I want to perform the following two functions at 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 get the difference in microseconds. Is there a way to do both at the same time? Any help would be appreciated.

+4
source share
4 answers

The following was recorded on the computer: this code sequentially displays 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() 
+4
source

This is (1) not possible at all (the "exact" part) and (2) not what Python fits well. If you really need precision microsecond execution, use C or ASM, but even closer than COpython's answer will be expected in two different processes within the 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) 
+4
source

I'm not sure if this will be done at exactly the same time, but I think it will bring 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) 
+2
source

CPython is essentially single-threaded (Google "Global Interpreter Lock"). To have even a theoretical chance, you will need a multi-core processor, but even then only an operating system that runs at a very low level can work, and even then you will need special equipment. What you ask for, in any practical sense, is impossible.

+1
source

Source: https://habr.com/ru/post/1447233/


All Articles