Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- import time
- from msvcrt import getch
- class myThread(threading.Thread):
- def __init__(self,name,switch):
- threading.Thread.__init__(self)
- self.switch = switch
- self.name = name
- def run(self):
- while self.switch == True:
- print('thread running, threads:', threading.enumerate())
- time.sleep(1)
- while True:
- key = getch()
- if key == b' ' and 'work' not in str(threading.enumerate()) :
- print('switching on, threads:',threading.active_count())
- thread = myThread('work', True)
- thread.start()
- elif key == b' ' and 'work' in str(threading.enumerate()):
- thread.switch = False
- thread.join()
- print('swithed off, threads:', threading.enumerate())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement