Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
test thread by Chou
#!/usr/bin/env python
from threading import Thread
import time
class Man(object):
def __init__(self):
self.switch = True
def stop(self):
self.switch = False
def run(self):
while self.switch:
print ('eating')
time.sleep(0.1)
startTime = time.time()
switch = True
Derek = Man()
process = Thread(target=Derek.run)
process.start()
while switch:
nowTime = time.time()
duration = nowTime - startTime
switch = False if duration>3 else True
time.sleep(0.5)
print ('using cell phone')
Derek.stop()
process.join()
print ("It's finished")
print ('thread is off' if not process.isAlive() else 'thread is still on')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.