如何使用线程一次读写一个文件

时间:2016-08-28 20:27:52

标签: python-3.x

我要打开N文件,我想获取N-new文件。我希望通过使用线程来防止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()

1 个答案:

答案 0 :(得分:0)

问题解决了:

from tkinter import *
from tkinter import ttk
from datetime import datetime as dt
import threading, time


def th():
         global mythread, cro
         mythread = threading.Thread(target=ok)
         cro = threading.Thread(target=cronou)
         mythread.start()

def aprifil2(z):
                      global m, state_crono, zero
                      s=0
                      while(s<10000000):
                              s+=1
                      print(s, z)
                      scr()
                      print(crono.get())
                      print('\n')
                      zero = dt.now()-dt.now()
                      if(z==m[len(m)-1]):
                                         state_crono = False
                                         time.sleep(0.01)
                                         crono.set('')

def scr():
          print('test\n')

def ok():
          global i, m, state_crono
          state_crono = True
          cro.start()
          m=['file1','file2', 'file3']
          for z in m:
                     aprifil2(z)

def cronou():
            global zero
            zero = dt.now()-dt.now()
            while(state_crono == True):
                                       ti = dt.now()
                                       time.sleep(0.01)
                                       tf = dt.now()
                                       zero += tf-ti
                                       crono.set(str(zero)[0:10])

finestra= Tk()
button_cerca = ttk.Button(finestra, text = "avvia", command = th)
button_cerca.pack()
button_a = Entry(finestra)
button_a.pack()
crono = StringVar()
tex_cronometro = Label(finestra, textvariable=crono, font=("Helvetica",11,"bold"))
tex_cronometro.pack()
finestra.mainloop()