Home > other >  Python futures. ProcessPoolExecutor - split () use of the file
Python futures. ProcessPoolExecutor - split () use of the file

Time:10-22

Usage:
1. The
To run the program, to select a file first, when running with futures. ProcessPoolExecutor () as executor, statements, still pop up multiple folders selection box let me choose the folder

2. But I use with futures. ThreadPoolExecutor (2) the as executor: thread pool, there is no problem (pictured), consult how to use the correct use of with futures. ProcessPoolExecutor () as executor:


Source:
[code=
The import tkinter as tk
The from tkinter import filedialog
The import of OS, time
The from concurrent import futures
The window of the # select files
AppWin=tk. Tk ()
AppWin. Withdraw ()
Initdir=OS. Path. The split (OS) path) realpath (__file__) [0]
PchFiles=filedialog. Askopenfilenames (parent=appWin, initialdir=initdir, title="both Please select PCH file to open, wining one can be selected")
T0=time. Time ()
Dirpath=OS. Path. The split (pchFiles [0]) [0]
PchFiles=pchFiles [0]
Mkfile=dirpath + '/' + OS. The path. The basename (pchFiles) [: - 4]
OS. Makedirs (mkfile)
# open PCH file
With the open (pchFiles) as obj:
Lines=obj. Readlines ()
# get condition name
Labels=[]
For the line in lines:
If "$LABEL=" in line:
Label=line] [11: - 1
If the label not in labels:
Labels. Append (label)
# save a file
Def save_oneFile (label) :
Global mkfile, lines
Writepath=mkfile + + label '/' + ' '+' PCH '
Obj=open (writepath, 'a')
I=1
For the line in lines:
I +=1
If $" TITLE="in line:
If label lines in [I + 2] :
Bol=True
The else:
Bol=False
If bol:
Obj. Write (line)
Obj. Close ()
# # del lines release the variable
The 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) the as executor:
With futures. ProcessPoolExecutor (1) as executor:
Res=executor. The 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 {: 2 f} s'
Print (MSG) format (count, elapsed))
# if __name__=="__main__ ':
The main (download_many)
T1=time. Time ()
Print (t1 - t0)
] [/code]

  • Related