Is using the args syntax in Python multiprocessing always necessary?
Question: I am learning up on methods of multiprocessing in Python and have found myself
with a question. Consider the following example:

    
    
    import multiprocessing as mp
    
    def worker(n):
        print('worker %d' % n)
        return
    
    if __name__ == '__main__':
        jobs = []
        for i in range(5):
            p = mp.Process(target = worker, args = (i,))
            jobs.append(p)
            p.start()
    

This is how the documentation that I am following uses `Process`.

Is it necessary to user `args = (i,)`? I have never seen this syntax in Python
before and it seems strange. I tested, and this works exactly the same:

`p = mp.Process(target = worker(i))`

Is there any reason that I should avoid that? Thanks for any help.


Answer: 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.

