I am struggling to understand the difference between run()
and start()
. According to the documentation, run()
method invokes the callable object passed to the object's constructor, while start()
method starts the process and can be called only once.
I tried an example below:
def get_process_id(process_name):
print process_name, os.getpid()
p1 = multiprocessing.Process(target=get_process_id, args=('process_1',))
p2 = multiprocessing.Process(target=get_process_id, args=('process_2',))
p1.run()
p2.run()
p1.start()
p2.start()
The results are below:
process_1 35138
process_2 35138
process_1 35141
process_2 35142
When I use run()
, it shows that p1
and p2
uses the same process. But when I use start()
, they give the two difference ones. Is it because calling run()
doesn't have anything to do with the process that calls it but just calling the function (which is get_process_id
in this example)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…