Python multiproccesing.Procces does not run in parallel
Question:
Why do processes of a code below not work in parallel?
When I am running the code I am waiting that it will run in parallel but it first waits when the first process will finish, then the second and so on. Nothing runs in parallel. Probably I have a blind spot in execution Python code in parallel.
import multiprocessing
import time
def run1(name):
print("Proccess", name, "has started")
time.sleep(2)
print("Proccess", name, "has finished")
def run2(name):
print("Proccess", name, "has started")
time.sleep(3)
print("Proccess", name, "has finished")
start_time = time.time()
pr1 = multiprocessing.Process(target=run1('A'), )
pr1.start()
print('-')
pr2 = multiprocessing.Process(target=run1('B'))
pr2.start()
print('-')
pr3 = multiprocessing.Process(target=run2('C'))
pr3.start()
print('-')
end_time = time.time()
print(end_time - start_time)
Answers:
Because instead of passing a target function
You call it…
Target=func
Not target=func ()
Why do processes of a code below not work in parallel?
When I am running the code I am waiting that it will run in parallel but it first waits when the first process will finish, then the second and so on. Nothing runs in parallel. Probably I have a blind spot in execution Python code in parallel.
import multiprocessing
import time
def run1(name):
print("Proccess", name, "has started")
time.sleep(2)
print("Proccess", name, "has finished")
def run2(name):
print("Proccess", name, "has started")
time.sleep(3)
print("Proccess", name, "has finished")
start_time = time.time()
pr1 = multiprocessing.Process(target=run1('A'), )
pr1.start()
print('-')
pr2 = multiprocessing.Process(target=run1('B'))
pr2.start()
print('-')
pr3 = multiprocessing.Process(target=run2('C'))
pr3.start()
print('-')
end_time = time.time()
print(end_time - start_time)
Because instead of passing a target function
You call it…
Target=func
Not target=func ()