I am using multiprocessing, and generating a pandas DataFrame with each process. I would like to merge them together and output the data. The following strategy seems almost work, but when trying to read in the data with df.read_csv() it only uses the first name as a column header.
from multiprocessing import Process, Lock
def foo(name, lock):
d = {f'{name}': [1, 2]}
df = pd.DataFrame(data=d)
lock.acquire()
try:
df.to_csv('output.txt', mode='a')
finally:
lock.release()
if __name__ == '__main__':
lock = Lock()
for name in ['bob','steve']
p = Process(target=foo, args=(name, lock))
p.start()
p.join()
multiprocessing.Queueto pass your end result back to the originating process, and leave the master process in charge of combining things.