Here is the code I have:
import pandas as pd
import multiprocessing as mp
CPU = 4
inp = pd.DataFrame({ 'col': ['a', 'b'] })
def test(dataframe):
df = dataframe.copy()
def worker(data):
print('worker')
def callback(data):
print('callback')
pool = mp.Pool(CPU)
for idx, row in df.iterrows():
print((idx, row['col']))
pool.apply_async(worker, args=[(idx, row['col'])], callback=callback)
pool.close()
pool.join()
return df
test(inp)
It works as expected if I run in the upper scope (without enclosing in the test
function), but after enclosing it in another function - they just are not called.
Here is output I receive with test
function:
(0, 'a')
(1, 'b')
Without:
(0, 'a')
(1, 'b')
worker
worker
callback
callback
So the question is - how can I make it work inside another function?