Difference between Process.run() and Process.start()

Question:

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)?

Asked By: Amadeus

||

Answers:

You’re exactly right. As it describes in the docs, run() is the entry point of the new thread created by start().

Answered By: robinsax

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.

Answered By: Darkonaut

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.

Answered By: neddstarkk
hi_man == None

if you_crazy:
    set_Null =0

else:
    set_Null =1
Answered By: 3bd2lra7man