python多处理
此脚本创建一个数据框并定义一个对象,然后尝试使用该数据框中的数据制成的对象填充列表。
通过将数据帧分为2部分并在每个部分上进行多处理来实现此目的
没有多处理功能就可以很好地工作,但是当我尝试多处理功能时,即使只有1个进程,部分代码似乎还是会抢先得到执行,而在只应执行一次的情况下也会重复执行
evidence of it running + it printing stuff from the end repeatedly
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)