使用問題:
1.

運行程式的時候,先手動選擇一個檔案,當運行到with futures.ProcessPoolExecutor() as executor:陳述句時,仍然彈出多個檔案夾選擇框讓我選擇檔案夾
2.但我使用with futures.ThreadPoolExecutor(2) as executor:執行緒池就沒有問題(如下圖),請教怎么才能使用正確使用with futures.ProcessPoolExecutor() as executor:

源代碼:
[code=
import tkinter as tk
from tkinter import filedialog
import os,time
from concurrent import futures
#選擇檔案的視窗
appWin = tk.Tk()
appWin.withdraw()
initdir = os.path.split(os.path.realpath(__file__))[0]
pchFiles = filedialog.askopenfilenames(parent=appWin,initialdir=initdir,title="Please select PCH file to open, more than one can be selected")
t0 = time.time()
dirpath = os.path.split(pchFiles[0])[0]
pchFiles = pchFiles[0]
mkfile = dirpath + '/' + os.path.basename(pchFiles)[:-4]
os.makedirs(mkfile)
#打開pch檔案
with open(pchFiles) as obj:
lines = obj.readlines()
#取得工況名稱
labels = []
for line in lines:
if "$LABEL =" in line:
label = line[11:-1]
if label not in labels:
labels.append(label)
#保存1個檔案
def save_oneFile(label):
global mkfile,lines
writepath = mkfile +'/'+ label +'.'+'pch'
obj = open(writepath,'a')
i = -1
for line in lines:
i += 1
if "$TITLE =" in line:
if label in lines[i+2]:
bol = True
else:
bol = False
if bol:
obj.write(line)
obj.close()
#del lines #釋放變數
return label
#['MODE', 'R2_pave belgian', 'R3_Band', 'R4_MidBelgian', 'R5_Ropes', 'R6_Washboard', 'R8_Cobblestone']
def download_many(labels):
workers = len(labels)
#with futures.ThreadPoolExecutor(2) as executor:
with futures.ProcessPoolExecutor(1) as executor:
res = executor.map(save_oneFile, sorted(labels))
return len(list(res))
def main(download_many):
t2 = time.time()
count = download_many(labels)
elapsed = time.time() - t0
msg = '\n{} flags downloaded in {:.2f}s'
print(msg.format(count, elapsed))
#if __name__ == '__main__':
main(download_many)
t1 = time.time()
print(t1 - t0)
][/code]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/183212.html
上一篇:關于UAV的信道估計思考
下一篇:5G
