2
votes

I am trying to learn multiprocessing in python. I wrote 2 simple function and wanted to run both in parallel. I am getting TypeError: 'int' object is not callable.

Here is the code I have:

from multiprocessing import Process
def add(a):
    t=0
    for i in range(a):
        t=i+1
    return t
def subtract (b):
    q=0
    for j in range(b):
        q=j+1
    return (q)

a=100000000
b=100000000

p1 = Process(target=add(a))
p1.start()
print("r")
p2 = Process(target=subtract(b))
p2.start()
print("q")

p1.join()
p2.join()

This is the error log I get:

Process Process-24: TypeError: 'int' object is not callable Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap self.run() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 99, in run self._target(*self._args, **self._kwargs) r Process Process-25: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap self.run() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 99, in run self._target(*self._args, **self._kwargs) TypeError: 'int' object is not callable

Thanks.

1

1 Answers

3
votes

The target argument to Process needs to be a function not a function call. Try:

Process(target=add, args=(a,)) 

Instead.

From the docs:

target is the callable object to be invoked by the run() method. It defaults to None, meaning nothing is called. ... args is the argument tuple for the target invocation.

In your invocation (p1 = Process(target=add(a))) you are passing the result of the add() function as the target callback, instead of the add() function itself.