Threading from a class

MRAB python at mrabarnett.plus.com
Sat Oct 17 21:27:35 EDT 2009


Someone Something wrote:
> anyone?
> 
> On Sat, Oct 17, 2009 at 4:26 PM, Someone Something <fordhaivat at gmail.com 
> <mailto:fordhaivat at gmail.com>> wrote:
> 
>     I'm trying to write a IRC client that has to have a method inside
>     class Client that has to start a new thread that goes to run() which
>     is in the same class. I'm not really understanding all the threading
>     tutorials i've found. Can someone help?
> 
>     p.s. trying to use the threading module, not the thread module.
> 
# Written for Python v2.6

import threading
import time

class Client(object):
     def test_thread(self):
         print "starting thread"
         self.thread = threading.Thread(target=self.run)
         self.thread.start()
         print "sleeping for 6 seconds"
         time.sleep(6)
         print "woken up"
         self.thread.join()
     def run(self):
         for i in range(5):
             print i
             time.sleep(1)

c = Client()
c.test_thread()



More information about the Python-list mailing list