I do not want to share but just send a DataFrame from one process to another.
The primary DataFrame is cutted into pieces and each piece is processed by a separate process (in the meaning of pythons multiprocessing) on its own CPU core. After the "child" processes are finished they should send back the resulting data frames to concat them together again.
But in my example I reach the END of the script but the DataFrames are never send back via the multiprocessing.Queue object.
#!/usr/bin/env python3
import multiprocessing
import pandas as pd
def worker(df, queue):
print(multiprocessing.current_process())
# create new column
df['X'] = df.b + '-' + df.c
# modify existing column
df.d = df.d.apply(lambda x: x.upper())
# send it back to main process
queue.put(df) # tried .copy() also!
if __name__ == '__main__':
print(pd.__version__)
# initial data
df = pd.DataFrame({
'a': ['A', 'A', 'B', 'B'],
'b': list('XXXX'),
'c': list('6218'),
'd': ['zwei', 'zwei', 'vier', 'neuen']
})
# slice the data frame
df_parts = [
df.iloc[:2].copy(),
df.iloc[2:].copy()
]
processes = []
queue = multiprocessing.Queue()
for i in range(len(df_parts)):
p = multiprocessing.Process(target=worker,
args=(df_parts[i], queue) )
processes.append(p)
p.start()
for p in processes:
p.join()
while queue.full():
print(queue.get())
print('END')
I am aware that sending data between multiprocessing.Process instances is pickled in the background. But I do not receive any error about that.
The output:
1.2.5
<Process name='Process-2' parent=14112 started>
<Process name='Process-1' parent=14112 started>
END
>>>