代码如下,求修改为多进程计算,单线程效率太低了。
import tkinter as tk
from tkinter import filedialog, dialog
from tkinter.filedialog import askdirectory
from tkinter.messagebox import *
import pandas as pd
import os
import time
import threading
class Mshot: ####节点仪施工可合成单炮
def __init__(self):
self.setup_UI()
def open_bigspsfile(self):
filebig_path = filedialog.askopenfilename(title='选择关系文件', filetypes=[('All Files', ['.xps','x'])])
self.bigpath.set(filebig_path)
def open_bigspsfile2(self):
filebig_path2 = filedialog.askopenfilename(title='选择节点仪表格', filetypes=[('All Files', ['.xlsx','xls'])])
self.bigpath2.set(filebig_path2)
def getValueBig(self):
self.filebig_path = self.bigpath.get()
return self.filebig_path
def getValueBig2(self):
self.filebig_path2 = self.bigpath2.get()
return self.filebig_path2
def mat_fileThread(self):
self.td2=threading.Thread(target=self.mat_file)
self.td2.setDaemon(True) ###守护线程,主界面关闭,子线程也关闭
self.td2.start()
def mat_file(self):
self.buttonMatch['state'] = tk.DISABLED
spsfile=self.getValueBig()
jdfile=self.getValueBig2()
if len(spsfile)==0 or len(jdfile)==0:
self.buttonMatch['state'] = tk.NORMAL
save_path=os.path.splitext(spsfile)[0]
time_start = time.time()
##############读取sps文件信息##############################
datal = []
for line in open(spsfile,'r'):
datal.append(line)
############计算头卡行数########
i = 0
while datal[i][0] == 'H':
i = i + 1
ii = i ##计算头卡行数
###########读取关系文件获得文件号与桩号对应关系####
vl = datal[ii:]
xlenthl = len(vl)
datalor=[]
#######################读取关系文件信息################
for i in range(0, xlenthl):
datalor.append([int(vl[i][10:15]),int(float(vl[i][17:24])),int(vl[i][28:34]),int(vl[i][38:43]),\
int(vl[i][43:48]),int(float(vl[i][49:59])),int(vl[i][62:66]),int(vl[i][72:76])])
pddatal3=pd.DataFrame(datalor)
pddatal3.columns=['ffid','shotline','shotpoint','chanfrom','chanto','rline','rpointfrom','rpointto']
pddatal3['chan_count']=pddatal3['chanto']-pddatal3['chanfrom']+1
#############################放炮文件号################
fired=pddatal3['ffid'].copy()
fired.drop_duplicates( keep='first', inplace=True) ##指定列去重,
fired.reset_index(drop=True, inplace=True) ###########重置index
fired=pd.DataFrame(fired)
fired['状态']='不完整'
del datal,vl,datalor
########################读取节点仪对应的检波点数据##########################
jd = pd.read_excel(jdfile)
jd.drop_duplicates(subset=['检波线','检波点'], keep='first', inplace=True)
###############################计算炮点是否可合成###########################
for i in range(len(fired)):
tempdata=pddatal3.loc[pddatal3['ffid']==fired['ffid'][i]]
tempdata.reset_index(drop=True, inplace=True) ###########重置index
chansum=tempdata['chan_count'].sum()
chantemp=0
for j in range(len(tempdata)):
tempjd=jd.loc[(jd['检波线']==tempdata['rline'][j])&
(jd['检波点']>=tempdata['rpointfrom'][j])&
(jd['检波点']<=tempdata['rpointto'][j])]
chantemp=chantemp+len(tempjd)
if chantemp==chansum:
fired.iat[i,1]= '完整'
fired.to_excel(save_path+'_可合成单炮统计.xlsx',index=None)
time_end=time.time()
timecost = str(round(time_end-time_start,4))
print('耗时 '+timecost+' S')
result2 = showinfo('提示', '结果保存在sps目录下!')
self.buttonMatch['state'] = tk.NORMAL
def setup_UI(self):
root = tk.Tk()
root.geometry('500x100+600+300')
root.title('节点仪施工可合成单炮统计')
self.bigpath= tk.StringVar(root)
self.bigpath2= tk.StringVar(root)
label1 = tk.Label(root, text="1、施工sps:")
label2 = tk.Label(root, text="2、节点仪表格:")
label1.grid(row=0)
label2.grid(row=1)
self.entry1 = tk.Entry(root,width=40,textvariable = self.bigpath)
self.entry2 = tk.Entry(root, width=40,textvariable = self.bigpath2)
self.entry1.grid(row=0, column=1)
self.entry2.grid(row=1, column=1)
buttonForFile = tk.Button(root, text="浏览文件", command=self.open_bigspsfile)
buttonRun = tk.Button(root, text="浏览文件", command=self.open_bigspsfile2)
buttonForFile.grid(row=0, column=2)
buttonRun.grid(row=1, column=2)
self.buttonMatch = tk.Button(root, text="3、数据处理", command=self.mat_fileThread)
self.buttonMatch.grid(row=4, column=1)
root.mainloop()
if __name__ == '__main__':
app = Mshot()
我想要达到的结果
1、全部核心都用上。
2、自动划分任务。
测试数据百度云盘
链接:https://pan.baidu.com/s/1Cjsu0dIVcpTIlHQU3Mbs3Q?pwd=gnku
提取码:gnku