Created
January 27, 2016 15:44
-
-
Save liuderchi/60838ffead66a3b22712 to your computer and use it in GitHub Desktop.
test thread by Chou
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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