[Tutor] How to use threads ?

Gwyn Evans gwyn.evans at gmail.com
Thu Mar 3 15:31:55 CET 2005


On Thu, 3 Mar 2005 16:04:28 +0200, Mark Kels <mark.kels at gmail.com> wrote:
> 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 ??

Hi,
  The one thing that stands out is that when you subclass Thread as
you do, you need to override the 'run' method, which will get called
as a result of you calling 'start()'.
  You're calling start(), but you've not got a run method, so the new
thread doesn't call your code.  Instead you've got your own call to
'scanner()', that is running under the main thread.

  Rename 'scanner()' to 'run()', remove the call to 'scanner()' and
see how that looks.

/Gwyn


More information about the Tutor mailing list