Here's a quick way for you to prove that it isn't the same thing. Change your worker(i)
function to this:
import time
def worker(n):
print('worker %d' % n)
time.sleep(1)
return
When you call this the first way, you'll notice that you still get all 5 prints at the same time right at the beginning. When you do it your second way, you'll see that you get all 5 prints staggered, with a second between each one.
Think about what you're trying to set up: 5 independent processes are each being started up at about the same time, each print at about the same time, and then each wait about a second, but the total time elapsed is only a little more than a second. This is what you want to have happen.
Here's the key point: target = worker
sets target
to be the worker
function, and args = (i,)
sets args
to be a single element tuple containing i
. On the other hand, target = worker(i)
calls worker(i)
and sets target
to be the value that the function returns, in this case None
. You're not really using multiprocessing
at all when you do it the second way. If you have a really time consuming task that you want to split across multiple processes, then you'll see no improvement when it's done the second way.
Basically, anytime you have func(args)
, you're going to be calling the function and getting its return value, whereas when you pass func
and args
separately, you allow the multiprocessing
package to work its magic and make those function calls on independent processes. Setting the target to be func(args)
will just call the function in the main process, losing you any benefits from multiprocessing in the first place.