我有N文件打開,我想獲得N新文件。我想通過使用線程來處理一個文件,以防止GUI tkinter凍結。在處理文件的實際情況下,我需要等待大約1-2分鐘。如何通過使用線程一次讀取和寫入一個文件
open file1-->read-->create new file1-->close file
open file2-->read-->create new file2-->close file
open file3-->read-->create new file3-->close file
在下面的測試代碼中,在讀寫模式下沒有打開文件,我使用了一個簡單的列表。在while循環如果計數是一個很大的數字,我沒有正確的結果:
#I expect an output like:
#100000000 file1 test release
#100000000 file2 test release
#100000000 file3 test release
#Indeed I obtain strange result like file3 as first result not in third position
from tkinter import filedialog
from tkinter import *
from tkinter import ttk
import threading, time
def th(lol):
global mythread, lock
mythread = threading.Thread(target=aprifil2, args=(lol,))
mythread.start()
def aprifil2(lol):
global lock, m
lock.acquire()
try:
s=0
while(s<100000000):
s+=1
print(s, i)
scr()
finally:
print('release\n')
try:
lock.release()
except RuntimeError:
print('end')
def scr():
print('test\n')
def ok():
global lock, i
lock = threading.Lock()
m=['file1','file2', 'file3']
for i in m:
lol=i
th(lol)
time.sleep(1)
finestra= Tk()
button_cerca = ttk.Button(finestra, text = "avvia", command = ok)
button_cerca.pack()
button_a = Entry(finestra)
button_a.pack()
你的意思是你想要的3個文件被依次處理?在這種情況下,您必須創建1個線程並處理其中的3個文件,而不是3個線程。 –
是,依次。一個線程是enoght,但我需要tkinter GUI不凍結...無論如何,我不明白我的錯誤。 – lausent