multiprocessing.Process does not initiate the parallel run of functions by start()
I don't understand how to get multiprocessing.Process
started. I used the following example code:
import random, time
import multiprocessing
开发者_开发问答def frz(no, tm):
a = 'start ' + str(no)
print(a)
time.sleep(tm)
a = str(no) + '=====> '+ str(tm) +'\n'
print(a)
if __name__ == '__main__':
freeze1 = random.randint(1, 5)
freeze2 = random.randint(1, 5)
freeze3 = random.randint(1, 5)
freeze4 = random.randint(1, 5)
freeze5 = random.randint(1, 5)
trd1 = multiprocessing.Process(target=frz, args=(1, freeze1))
trd2 = multiprocessing.Process(target=frz, args=(2, freeze2))
trd3 = multiprocessing.Process(target=frz, args=(3, freeze3))
trd4 = multiprocessing.Process(target=frz, args=(4, freeze4))
trd5 = multiprocessing.Process(target=frz, args=(5, freeze5))
#threading
trd1.start()
trd2.start()
trd3.start()
trd4.start()
trd5.start()
When multiprocessing.Process
is being replaced by threading.Thread
the function works fine, but with multiprocessing nothing seems to happen.
When I ran your code, this is what I saw:
start 5 start 4 start 3 start 2 start 1 2=====> 1 5=====> 3 3=====> 3 4=====> 4 1=====> 5
Is this what you were expecting?
精彩评论