python通过数据库进行多处理

python通过数据库进行多处理,python,multiprocessing,Python,Multiprocessing,python多进程 此脚本创建一个数据帧并定义一个对象,然后尝试用该数据帧中的数据创建的对象填充列表 它通过将数据帧分成两个部分并对每个部分进行多处理来实现这一点 没有多进程处理,它工作得很好,但当我尝试多进程处理时,即使只有一个进程,代码的某些部分似乎也会跳转,提前执行,当它们只应该执行一次时,也会重复执行 import random import pandas as pd import multiprocessing as mp import time class car_term():

python多进程

此脚本创建一个数据帧并定义一个对象,然后尝试用该数据帧中的数据创建的对象填充列表

它通过将数据帧分成两个部分并对每个部分进行多处理来实现这一点

没有多进程处理,它工作得很好,但当我尝试多进程处理时,即使只有一个进程,代码的某些部分似乎也会跳转,提前执行,当它们只应该执行一次时,也会重复执行

import random
import pandas as pd
import multiprocessing as mp
import time

class car_term(): #object to go into list 
    def __init__(self, capcode, miles,months , cmprice, fmprice ):
        self.capcode = capcode
        self.months = months
        self.miles = miles
        self.cmprice = cmprice
        self.fmprice = fmprice

df_final = pd.DataFrame({'capcode':[],'months':[],'mileage':[],'cm':[],'fm':[]})

for i in range (200): # making dataframe to get data from
    df_final = df_final.append(pd.DataFrame({'capcode':[i],'months':[random.randint(1, 12)],'mileage':[random.randint(0, 10000)],'cm':[random.randint(5, 700)],'fm':[random.randint(15, 710)]}))


all_deals=[] # this is the list i want to put my objects into
def get_car_terms(data,mdb1,all_deals1):
    all_deals1.append(car_term(mdb1['capcode'][data],mdb1['mileage'][data],mdb1['months'][data],mdb1['cm'][data],mdb1['fm'][data])) # i make the objects with the dataframe like this
all_deals1a=[]   # individual lists for each proccessor

print("yo1")
if __name__ == "__main__":
    print('1')
    if df_final.shape[0] != 0:
        print('2')
        df_final = df_final.reset_index(drop=True)
        print(df_final.head())
        for i in range(int(df_final.shape[0]/4)):

            ############# the problem is the get_car_terms function doesnt run below
            p1 = mp.Process( target = get_car_terms , args =(i+((df_final.shape[0]/4)*1), df_final,all_deals1a)) # each cpu sorts a quater of the dataframe into my objects list


            p1.start()

            time.sleep(10000)
            p1.join()
         #   get_car_terms(i+((df_final.shape[0]/n_cpus)*2), df_final,all_deals1a)


            print(" ppp ")
    all_deals.append(all_deals1a)   # group lists together


print("we did it")
print(len(all_deals))  # this should have 200 of my objects in it... it doesnt
for i in all_deals:
    print(len(i))
    for j in i:
        print(j.miles)