[Tutor] How to use threads ?

Mark Kels mark.kels at gmail.com
Thu Mar 3 15:04:28 CET 2005


On Wed, 2 Mar 2005 14:48:11 -0800 (PST), Shitiz Bansal
<shitizb at yahoo.com> wrote:
> The thread finishes when:
> 1.The run method finishes.
> 2.In case of the loop- you may -
> 
> >>> import threading
> >>> class abc(threading.Thread):
>         def __init__(self):
>                 threading.Thread.__init__(self)
>                 self.cont_flag=1
>         def run(self):
>                 while self.cont_flag:
>                         pass
> 
> >>> abcd=abc()
> >>> abcd
> <abc(Thread-1, initial)>
> >>> abcd.start()
> >>> abcd
> <abc(Thread-1, started)>
> >>> abcd.cont_flag=0
> >>> abcd
> <abc(Thread-1, stopped)>

But for some reason it keeps working after its job is done in my app...
Here is the class:
(the app itself got some Entrys to get the input and a Button to start
the go() and stop() functions)

class scan(threading.Thread):
    def _init_(self):
        threading.thread._init_(self)
    def scanner(self):
        self.open_counter=0
        self.flag='scan'
        try:
            host=host_e.get()
            start_port=int(start_port_e.get())
            end_port=int(end_port_e.get())
        except ValueError:
            tkMessageBox.showerror("Bad input","You must enter a vaild
host IP and port numbers.")
            pass
        else:
            start.config(text="Stop",command=stop)
            root.update()
            result.insert(END,"Scanning "+str(host)+"...\n\n")
            root.update()
            while start_port<=end_port and self.flag=='scan':
                self.sk=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sk.settimeout(0.01)
                try:
                  self.sk.connect((host,start_port))
                except:
                    pass
                else:
                    result.insert(END,str(start_port)+"\n")
                    root.update()
                    self.open_counter=self.open_counter+1
                    self.sk.close()
                start_port=start_port+1
            if self.flag=='scan':    
                result.insert(END,"\nDone !!\nFound
"+str(self.open_counter)+" opened ports")
                root.update()
                start.config(text="Scan",command=go)
                root.update()
            elif self.flag=='stop':
                result.insert(END,"\n Scan stopped.")
                start.config(text="Scan",command=go)
                root.update()
app=scan()

Here is the go() function, which starts the thread:
def go():
    app.start()
    app.scanner()

And here is the stop() function that should end the thread and the
scan (and for some reason it ends only the scan loop):
def stop():
    app.flag='stop'

You got any Idea whats wrong here ??

Thanks allot.
-- 
1. The day Microsoft makes something that doesn't suck is probably the
day they start making vacuum cleaners.
2. Unix is user friendly - it's just picky about it's friends.
3. Documentation is like sex: when it is good, it is very, very good.
And when it is bad, it is better than nothing. - Dick Brandon


More information about the Tutor mailing list