Created
April 20, 2014 05:18
-
-
Save xebecnan/11105751 to your computer and use it in GitHub Desktop.
This file contains 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
def status_worker(body, last_modify): | |
try: | |
expire = time.time() + 10 | |
while time.time() < expire: | |
mtime = os.path.getmtime(STATUS_FILE) | |
if mtime > last_modify: | |
break | |
time.sleep(1) | |
if not os.path.exists(STATUS_FILE): | |
body.put(json.dumps({ | |
'status': '没有找到状态信息' | |
}) | |
else: | |
with open(STATUS_FILE, 'r') as f: | |
c = f.read().strip() | |
body.put(json.dumps({ | |
'status':c, | |
'last_modify': mtime, | |
})) | |
finally: | |
body.put(StopIteration) | |
@bottle.route('/chocobo/do/status', ['GET','POST']) | |
def get_status(): | |
last_modify = bottle.request.params.get('lm', 0) | |
try: | |
last_modify = int(last_modify) | |
except ValueError: | |
return {'status': '程序错误'} | |
body = Queue() | |
gevent.spawn(status_worker, body, last_modify) | |
return body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment