I am trying to understand how the callback
function in Pool.apply_async
works, and ran into the following issue. Outside of the callback, I define
results = []
and want to use the callback to append return values from the worker process to results
as the values become available.
The strange thing, though, is that this statement (in the callback) works :
results.append(t)
but
results += [t]
reports an unassigned reference error.
What is going on? Any insight will be greatly appreciated!
Here is the complete code :
from multiprocessing import Pool
import random
results = []
def f(t):
return t
def cb_append_result(t):
# reports "local variable 'results' referenced before assignment"
# results += [t]
# Okay
results.append(t) # works
if __name__ == '__main__':
pool = Pool()
t = random.random()
pool.apply_async(f,args=(t,),callback=cb_append_result)
pool.close()
pool.join()
print("Result is {}".format(results))
(ADDED) The issue has nothing to do with multiprocessing or callbacks (see answer below). But to fix the problem above, the callback function should be written as
def cb_append_result(t):
global results
results += [t]