Home > OS >  Function repetition speed up python (using multiprocessing??)
Function repetition speed up python (using multiprocessing??)

Time:10-18

I want to repeat same function using list component as input variable. I make a source below.

def sample_function(a,b,c):
    ~~~
    return A

list_list=pd.DataFrame()
q=2
r=3
for i in range(0,len(list)):
    p=check[i]
    result=sample_function(p,q,r)
    list_list=list_list.append(result)

But this work takes a lot of time....

How can I decrease time for this work?

check=['AA','BB','CC','DD',,,]

CodePudding user response:

You can do it using Pool from multiprocessing. Here is a simple example:

from multiprocessing import Pool
import pandas as pd

def sample_function(a,b,c):
    a=check[a]
    ~~~
    return result

list_list=pd.DataFrame()

list_list = []
pool = Pool(len(list))
results = pool.map(sample_function, range(0,len(list), q, r)

list_list = pd.concat(results)
  • Related