Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import threading
  2. import time
  3. from msvcrt import getch
  4.  
  5. class myThread(threading.Thread):
  6. def __init__(self,name,switch):
  7. threading.Thread.__init__(self)
  8. self.switch = switch
  9. self.name = name
  10.  
  11. def run(self):
  12. while self.switch == True:
  13. print('thread running, threads:', threading.enumerate())
  14. time.sleep(1)
  15.  
  16. while True:
  17. key = getch()
  18. if key == b' ' and 'work' not in str(threading.enumerate()) :
  19. print('switching on, threads:',threading.active_count())
  20. thread = myThread('work', True)
  21. thread.start()
  22.  
  23. elif key == b' ' and 'work' in str(threading.enumerate()):
  24. thread.switch = False
  25. thread.join()
  26. print('swithed off, threads:', threading.enumerate())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement