Describing run()
- The
run
method runs aThread
instancet
- The
run
method is invoked by thestart
method - By default, it calls the target function passed in the constructor
- This method can also be redefined in subclasses of
Thread
Example of run
# sleepy.py
>>> import logging
>>> import threading as t
>>> import time
>>> def sleep_thread(msg):
... print('thread started')
... print(msg)
... time.sleep(2)
... print('thread done')
>>> if __name__ == "__main__":
... print('main started')
... msg = 'i am sleepy'
... thd = t.Thread(target=sleep_thread, args=(msg,))
... print('main created thread')
... thd.start() # invokes thd.run() automatically
... print('main started thread')
... print('main done')
References
Previous
Next