简体   繁体   中英

How to use Multiprocessing in Pandas?

when i use multiprocessing in normal program its working fine in pandas,but when i use this multiprocessing time is not reducing.

i tried with normal code using multiprocessing

import multiprocessing
import time
import pandas as pd
start=time.time()

def square(df1):
    df1['M_threading'] = df1['M_Invoice_type']
def multiply(df4):
    df4['M_threading'] = df4['M_Invoice_type']

if __name__ == '__main__':
    df = pd.read_excel("filename.xlsx")
    df1 = df.loc[df['M_Invoice_type'] == 'B2B']
    df4 = df.loc[df['M_Invoice_type'] == 'B2BUR']
    p=multiprocessing.Process(target=square,args=(df1,))
    p1 = multiprocessing.Process(target=multiply, args=(df4,))
    p.start()
    p1.start()
    p.join()
    p1.join()
    print("Done")
    end=time.time()
    print(end-start)

I expect the output time of code 25sec, but the actual output is 51sec.

Try calling multiprocessing.Process with the two new dataframes created.

p = multiprocessing.Process(target=square,args=(df1,))
p1 = multiprocessing.Process(target=multiply, args=(df4,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM