This is the demo code from Python
from multiprocessing import Process, Manager
def f(d, l):
d[1] = '1'
d['2'] = 2
d[0.25] = None
l.reverse()
if __name__ == '__main__':
with Manager() as manager:
d = manager.dict()
l = manager.list(range(10))
p = Process(target=f, args=(d, l))
p.start()
p.join()
print(d)
print(l)
It works and outputs
{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
However, if I remove the d variable
from multiprocessing import Process, Manager
def f(l):
l.reverse()
if __name__ == '__main__':
with Manager() as manager:
l = manager.list(range(10))
p = Process(target=f, args=(l))
p.start()
p.join()
print(l)
It fails with the following output messages
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Process Process-2:
Traceback (most recent call last):
File "/home/yangzh/anaconda3/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/home/yangzh/anaconda3/lib/python3.6/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
TypeError: f() takes 1 positional argument but 10 were given
Why does the function f() require more than one argument? Using a dummy argument as a solution seems pretty stupid.