So your current code is actually failing because of this line:
print(f"Gets here for process name {multiprocessing.current_process().name()}")
it errors out as TypeError: 'str' object is not callable
, not because there is anything with the way you are calling multiply()
if you remove it:
import multiprocessing
results = []
def log_results(result):
results.append(result)
def multiply(x, y):
# print(f"Gets here for process name {multiprocessing.current_process().name()}")
return x * y
if __name__ == "__main__":
pool = multiprocessing.Pool()
numbers = [(1,1), (2,2), (3,3)]
for x, y in numbers:
print (f"Checking x {x} and y {y}")
pool.apply_async(multiply, (x, y), callback=log_results)
pool.close()
pool.join()
print(results)
It returns:
Checking x 1 and y 1
Checking x 2 and y 2
Checking x 3 and y 3
[1, 4, 9]
So if you actually isolate your print(f)
:
print(multiprocessing.current_process().name())
you get the error: TypeError: 'str' object is not callable
because
multiprocessing.current_process()
is actually a process object with name
as an attribute of the object which returns a string (thanks darkonaut) string. You are trying to call .name()
as a function, but it is an attribute.
So if you change your function to include .name
, instead of .name()
:
import multiprocessing
results = []
def log_results(result):
results.append(result)
def multiply(x, y):
print(f"Gets here for process name {multiprocessing.current_process().name}")
return x * y
if __name__ == "__main__":
pool = multiprocessing.Pool()
numbers = [(1,1), (2,2), (3,3)]
for x, y in numbers:
print (f"Checking x {x} and y {y}")
pool.apply_async(multiply, (x, y), callback=log_results)
pool.close()
pool.join()
print(results)
You return:
Checking x 1 and y 1
Checking x 2 and y 2
Checking x 3 and y 3
Gets here for process name ForkPoolWorker-1
Gets here for process name ForkPoolWorker-2
Gets here for process name ForkPoolWorker-3
[1, 4, 9]
Which is what you desire.