Folyamatjelző - Egyszerű alkalmazás vázlata szálak (threads) használatára




A szálak (threads) alkalmazására kézen fekvő példa az, amikor egy ftp serverről letöltünk egy állományt és a felhasználónak - hogy lássa mi történik - egy folyamatjelzővel (progressbar) kijelezzük, hogy hol tart folyamat. A következő kód a megvalósítás alapötletét mutatja be.

Folyamatjelzőként Michael Lange progressbar widget-jét fogom használni annyi módosítással, hogy a Tkinter.Frame osztály helyett a Tkinter.Toplevel osztályból származtatom le:


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
    
#! /usr/bin/env python
'''Michael Lange 
The Meter class provides a simple progress bar widget for Tkinter.

INITIALIZATION OPTIONS:
The widget accepts all options of a Tkinter.Frame plus the following:

    fillcolor -- the color that is used to indicate the progress of the
                 corresponding process; default is "orchid1".
    value -- a float value between 0.0 and 1.0 (corresponding to 0% - 100%)
             that represents the current status of the process; values higher
             than 1.0 (lower than 0.0) are automagically set to 1.0 (0.0); default is 0.0 .
    text -- the text that is displayed inside the widget; if set to None the widget
            displays its value as percentage; if you don't want any text, use text="";
            default is None.
    font -- the font to use for the widget's text; the default is system specific.
    textcolor -- the color to use for the widget's text; default is "black".

WIDGET METHODS:
All methods of a Tkinter.Frame can be used; additionally there are two widget specific methods:
    
    get() -- returns a tuple of the form (value, text)
    set(value, text) -- updates the widget's value and the displayed text;
                        if value is omitted it defaults to 0.0 , text defaults to None .
'''

import Tkinter

class Meter(Tkinter.Frame):
    def __init__(self, master, width=300, height=20, bg='white', fillcolor='orchid1',\
                 value=0.0, text=None, font=None, textcolor='black', *args, **kw):
        Tkinter.Frame.__init__(self, master, bg=bg, width=width, height=height, *args, **kw)
        self._value = value

        self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
                                    highlightthickness=0, relief='flat', bd=0)
        self._canv.pack(fill='both', expand=1)
        self._rect = self._canv.create_rectangle(0, 0, 0, self._canv.winfo_reqheight(), fill=fillcolor,\
                                                 width=0)
        self._text = self._canv.create_text(self._canv.winfo_reqwidth()/2, self._canv.winfo_reqheight()/2,\
                                            text='', fill=textcolor)
        if font:
            self._canv.itemconfigure(self._text, font=font)

        self.set(value, text)
        self.bind('', self._update_coords)

    def _update_coords(self, event):
        '''Updates the position of the text and rectangle inside the canvas when the size of
        the widget gets changed.'''
        # looks like we have to call update_idletasks() twice to make sure
        # to get the results we expect
        self._canv.update_idletasks()
        self._canv.coords(self._text, self._canv.winfo_width()/2, self._canv.winfo_height()/2)
        self._canv.coords(self._rect, 0, 0, self._canv.winfo_width()*self._value, self._canv.winfo_height())
        self._canv.update_idletasks()

    def get(self):
        return self._value, self._canv.itemcget(self._text, 'text')

    def set(self, value=0.0, text=None):
        #make the value failsafe:
        if value < 0.0:
            value = 0.0
        elif value > 1.0:
            value = 1.0
        self._value = value
        if text == None:
            #if no text is specified use the default percentage string:
            text = str(int(round(100 * value))) + ' %'
        self._canv.coords(self._rect, 0, 0, self._canv.winfo_width()*value, self._canv.winfo_height())
        self._canv.itemconfigure(self._text, text=text)
        self._canv.update_idletasks()

##-------------demo code--------------------------------------------##

def _demo(meter, value):
    meter.set(value)
    if value < 1.0:
        value = value + 0.005
        meter.after(50, lambda: _demo(meter, value))
    else:
        meter.set(value, 'Demo successfully finished')

if __name__ == '__main__':
    root = Tkinter.Tk(className='meter demo')
    m = Meter(root, relief='ridge', bd=3)
    m.pack(fill='x')
    m.set(0.0, 'Starting demo...')
    m.after(1000, lambda: _demo(m, 0.0))
    root.mainloop()
 


A feladat megoldása a következő lépésekből áll:

Nem készítek GUI-t, hogy a kód a lehető legrövidebb legyen. Ezért a server nevét, a felhasználónevet, a jelszót, a forráskönyvtár elérési útját, a letöltendő file nevét, a célkönyvtárat a Globals osztály attribútumaiban fogom tárolni (11-21. sorok).


A fileletöltés szála akkor szűnik meg, amikor teljesen letöltődött a file.
A folyamatjelző programszálában arról kell gondoskodni, hogy a value nevű változó a letöltött filerésznek megfelelő értéket vegyen fel és ezzel az értékkel hívjuk meg a folyamatjelző objektumunk set() metódusát amikor a szál megkapja a processzoridőt. Ezért a letöltött filerész méretét a ThreadProgressBar osztály run() metódusában (110-134. sorok) egy ciklusban fogjuk beolvasni a célkönyvtárból. (A ThreadProgressBar osztály run() metódusához a progressbar widget _demo() függvénye adja az ötletet.) A ciklusból akkor lépünk ki, amikor letöltöttük a file-t. Ez egyben a szál befejezését is jelenti.
A script főszálának végrehajtását a fileletöltés és a folyamatjelző szálak join() metódusának hívásával addig blokkoljuk, amíg a két szál meg nem szűnik.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    
#!/usr/bin/env python
# -*- coding: Utf-8 -*-

import sys
import os
import Tkinter
import ftplib
import threading

#--------------------------------------------------------------------------------------------
class Globals:
    #local parameters
    racine = '/home/peter/Documents/ftp_progressindicator/'     #destination path on the local pc
    fullPathName = ""						
    totalFileSize = 0
    #server parameters
    server = "mirrors.sth.sze.hu"
    user = "anonymous"
    password = "none"
    directory = "/linux/suse/i386/9.3"				#source directory on the ftp server
    fileName = "ChangeLog"					#file to download
#--------------------------------------------------------------------------------------------

#--------------------------------------------------------------------------------------------
'''Michael Lange 
The Meter class provides a simple progress bar widget for Tkinter.
http://tkinter.unpythonic.net/wiki/ProgressMeter

INITIALIZATION OPTIONS:
The widget accepts all options of a Tkinter.Frame plus the following:

    fillcolor -- the color that is used to indicate the progress of the
                 corresponding process; default is "orchid1".
    value -- a float value between 0.0 and 1.0 (corresponding to 0% - 100%)
             that represents the current status of the process; values higher
             than 1.0 (lower than 0.0) are automagically set to 1.0 (0.0); default is 0.0 .
    text -- the text that is displayed inside the widget; if set to None the widget
            displays its value as percentage; if you don't want any text, use text="";
            default is None.
    font -- the font to use for the widget's text; the default is system specific.
    textcolor -- the color to use for the widget's text; default is "black".

WIDGET METHODS:
All methods of a Tkinter.Frame can be used; additionally there are two widget specific methods:
    
    get() -- returns a tuple of the form (value, text)
    set(value, text) -- updates the widget's value and the displayed text;
                        if value is omitted it defaults to 0.0 , text defaults to None .
'''

class Meter(Tkinter.Toplevel):
    def __init__(self, master, width=300, height=20, bg='white', fillcolor='orchid1',\
                 value=0.0, text=None, font=None, textcolor='black', *args, **kw):
        Tkinter.Toplevel.__init__(self, bg=bg, width=width, height=height, *args, **kw)

        
        self._value = value

        self._canv = Tkinter.Canvas(self, bg=self['bg'], width=self['width'], height=self['height'],\
                                    highlightthickness=0, relief='flat', bd=0)
        self._canv.pack(fill='both', expand=1)
        self._rect = self._canv.create_rectangle(0, 0, 0, self._canv.winfo_reqheight(), fill=fillcolor,\
                                                 width=0)
        self._text = self._canv.create_text(self._canv.winfo_reqwidth()/2, self._canv.winfo_reqheight()/2,\
                                            text='', fill=textcolor)
        if font:
            self._canv.itemconfigure(self._text, font=font)

        self.set(value, text)
        self.bind('<Configure>', self._update_coords)

    def _update_coords(self, event):
        '''Updates the position of the text and rectangle inside the canvas when the size of
        the widget gets changed.'''
        # looks like we have to call update_idletasks() twice to make sure
        # to get the results we expect
        self._canv.update_idletasks()
        self._canv.coords(self._text, self._canv.winfo_width()/2, self._canv.winfo_height()/2)
        self._canv.coords(self._rect, 0, 0, self._canv.winfo_width()*self._value, self._canv.winfo_height())
        self._canv.update_idletasks()

    def get(self):
        return self._value, self._canv.itemcget(self._text, 'text')

    def set(self, value=0.0, text=None):
        #make the value failsafe:
        if value < 0.0:
            value = 0.0
        elif value > 1.0:
            value = 1.0
        self._value = value
        if text == None:
            #if no text is specified use the default percentage string:
            text = str(int(round(100 * value))) + ' %'
        self._canv.coords(self._rect, 0, 0, self._canv.winfo_width()*value, self._canv.winfo_height())
        self._canv.itemconfigure(self._text, text=text)
        self._canv.update_idletasks()
#--------------------------------------------------------------------------------------------

#--------------------------------------------------------------------------------------------
class ThreadProgressBar(threading.Thread):
       #def __init__(self, parent, prBar):
       """Animacios parameterek inicializalasa"""
       def __init__(self, prBar):
           threading.Thread.__init__(self)
           self.prBar_ = prBar
           self.value = 0.0
           self.prBar_.set('Starting demo...')	 

       def run(self):
           while 1:
               try:
                  self.fileSize = os.path.getsize(Globals.fullPathName)
               except OSError, err:
                  pass       		 #meg nem letezik a file
               else:
                  break

           while self.fileSize != Globals.totalFileSize:
               try:
                  self.fileSize = os.path.getsize(Globals.fullPathName)
               except OSError, err:
                  self.fileSize = 0.0    #meg nem letezik a file

               if self.fileSize != Globals.totalFileSize :  
                  if self.fileSize == 0.0:
                     self.value =0.0
                  else:
                     self.value = (0.0 + self.fileSize)/Globals.totalFileSize

                  self.prBar_.set(self.value)    
           else:
               self.prBar_.set(1, 'Demo successfully finished')
               self.prBar_.master.destroy()
#--------------------------------------------------------------------------------------------

#--------------------------------------------------------------------------------------------
class FileDownload:
    def __init__(self):
        "Incorporate file download functionality."
        
    def connectToServer(self):
        try:
             self.ftp = ftplib.FTP(Globals.server)                      #connect to host, default port
             self.ftp.login(Globals.user, Globals.password)             #user 'anonymous', passwd 'none'
             self.ftp.cwd(Globals.directory)                            #Change directory    
             Globals.totalFileSize = self.ftp.size(Globals.fileName)    #read filesize and store it in a global
        except ftplib.all_errors, err:
            print err
     
    def fileContent(self, fname_):
        #Downloads file
        try:
            f=open(Globals.racine+fname_,"w")	
            self.ftp.retrbinary("RETR " + fname_, f.write)
        except ftplib.all_errors, e:
            print "Error :", e
        
    def deconnectFromServer(self):
        self.ftp.quit()
#--------------------------------------------------------------------------------------------

#--------------------------------------------------------------------------------------------
class ThreadFileDownload(threading.Thread):
    def __init__(self, downL):
        threading.Thread.__init__(self)
        self.downL = downL	

    def run(self):
        self.downL.fileContent(Globals.fileName)
#--------------------------------------------------------------------------------------------


######################################################################
if __name__ == '__main__':
    title = 'Thread demo'
    root = Tkinter.Tk()
    root.title(title)
    root.withdraw()
    Globals.fullPathName = Globals.racine + Globals.fileName
    fileDownload = FileDownload()
    fileDownload.connectToServer()
    tfileDownload = ThreadFileDownload(fileDownload)
    m = Meter(root, relief='ridge', bd=3)
    tProgressBar = ThreadProgressBar(m)
    tfileDownload.start()
    tProgressBar.start()
    tfileDownload.join()
    tProgressBar.join()
    fileDownload.deconnectFromServer()
    root.mainloop()