From ddf40c02dcc7e78ef2f87e7cd0e127178f8a348a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C5=91ri=20S=C3=A1ndor?= Date: Sun, 30 Sep 2018 15:21:02 +0200 Subject: [PATCH] added python threading example --- lesson02/example3_threading.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lesson02/example3_threading.py diff --git a/lesson02/example3_threading.py b/lesson02/example3_threading.py new file mode 100644 index 0000000..7be67de --- /dev/null +++ b/lesson02/example3_threading.py @@ -0,0 +1,30 @@ +""" Example 3 +Threading +""" + +import threading +import time + + +class MyThread(threading.Thread): + """ Example thread class extending the base thread class + + Arguments: + threading {[type]} -- [description] + """ + + def run(self): + """ Run the thread and log it + """ + + print "{} started!".format(self.getName()) # Thread-x started! + time.sleep(1) # Pretend to work for a second + print "{} finished!".format(self.getName()) # "Thread-x finished!" + + +if __name__ == '__main__': + for x in range(4): # Four times... + mythread = MyThread(name="Thread-{}".format( + x + 1)) # ...Instantiate a thread and pass a unique ID to it + mythread.start() # ...Start the thread + time.sleep(.6) # ...Wait 0.6 seconds before starting another