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)?
To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.
As we have seen, the Pool allocates only executing processes in memory and the process allocates all the tasks in memory, so when the task number is small, we can use process class and when the task number is large, we can use the pool.
The multiprocessing Python module contains two classes capable of handling tasks. The Process class sends each task to a different processor, and the Pool class sends sets of tasks to different processors.
In this example, at first we import the Process class then initiate Process object with the display() function. Then process is started with start() method and then complete the process with the join() method. We can also pass arguments to the function using args keyword.
You are not supposed to call process.run()
explicitly. It's the method which invokes your specified target
function unless you override it when you subclass Process
. It normally gets called within the new child while it bootstraps. It does nothing else than calling the target function.
# multiprocessing.process.BaseProcess
def run(self):
'''
Method to be run in sub-process; can be overridden in sub-class
'''
if self._target:
self._target(*self._args, **self._kwargs)
When you call it in your parent process, it gets executed in your parent process like any other method.
process.start()
is the method which you're supposed to call in your parent to create the new process in the first place.
Invoking start()
will create a new thread and execute run()
in this new thread. Whereas, invoking run()
yourself will execute it in the current thread itself. Execution of run()
will not switch to a different thread. So it will execute its actions on the main thread itself.
You're exactly right. As it describes in the docs, run()
is the entry point of the new thread created by start()
.
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