Skip to content

Instantly share code, notes, and snippets.

@liuderchi
Created January 27, 2016 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuderchi/60838ffead66a3b22712 to your computer and use it in GitHub Desktop.
Save liuderchi/60838ffead66a3b22712 to your computer and use it in GitHub Desktop.
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