Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import threading
  4. import time
  5. #import paramiko
  6.  
  7. class MyThread(threading.Thread):
  8.     def __init__(self, name):
  9.         super(MyThread, self).__init__()
  10.         self.name = name
  11.         self.stoprequest = False
  12.  
  13.     def run(self):
  14.         print("{} started!".format(self.getName()))
  15.         while not self.stoprequest:
  16.             time.sleep(1)
  17.         print("{} stopped!".format(self.getName()))
  18.    
  19.     def stop(self, timeout=None):
  20.         self.stoprequest = True
  21.         super(MyThread, self).join(timeout)
  22.  
  23.     def say_hello(self):
  24.         print("Hello from {}".format(self.getName()))
  25.  
  26. def main():
  27.     threads = []
  28.     for x in range(4):
  29.         threads.append(MyThread(name="Thread-{}".format(x+1)))
  30.         threads[x].start()
  31.         time.sleep(.9)
  32.     terminate = False
  33.     while not terminate:
  34.         val = input("Enter thread number: ")
  35.         if val == 'q':
  36.             terminate = True
  37.         else:
  38.             threads[int(val)-1].say_hello()
  39.     for thread in threads:
  40.         thread.stop()
  41. if __name__ == '__main__':
  42.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement