Skip to content

Instantly share code, notes, and snippets.

@xebecnan
Created April 20, 2014 05:18
Show Gist options
  • Save xebecnan/11105751 to your computer and use it in GitHub Desktop.
Save xebecnan/11105751 to your computer and use it in GitHub Desktop.
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