1

I'm learning multiprocessing module. Here are examples where function calling is strange at least for me. Passing the arguments ends with comma (seems like last argument is empty or forgotten or something weird) e.g.

#!/usr/bin/python

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes

    result = pool.apply_async(f, (10,))    # evaluate "f(10)" asynchronously
    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow

Or this

#!/usr/bin/python

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes

    result = pool.apply_async(f, [10,])    # evaluate "f(10)" asynchronously
    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow  

Or this, where calling is done using named arguments

#!/usr/bin/python

from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print q.get()    # prints "[42, None, 'hello']"
    p.join()

There are also examples where passing argument is done "classic" (arg1,arg2) way, I've tried modify above examples and delete the last comma but I gen an error. First I was thinking that those missed arguments are used with Process class and those "classic" arguments are used with Pool but there are exception on mentioned site. What does this convention mean? What is difference between (arg1,) and (arg1)?

EDIT As it was mentioned in the comments type((1,)) returns tuple where type((1)) returns int. But f(x) function requires tuple?

Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122

0 Answers0