-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottle-rest-demo.py
62 lines (49 loc) · 1.38 KB
/
bottle-rest-demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# !/usr/bin/env python
# -*- coding:utf-8 -*-
from gevent.coros import RLock
from bottle import route, Bottle, run
lock = RLock()
class Counter(object):
counts = {}
def get(self, app, id):
key = '_'.join((app, id))
count = self.counts.get(key, 0)
return {'data' : count}
def inc(self, app, id, step):
key = '_'.join((app, id))
count = self.counts.get(key, 0)
if lock.acquire(0) is False:
return {'data' : count}
try:
count += 1
self.counts[key] = count
finally:
lock.release()
def delete(self, app, id):
key = '_'.join((app, id))
if lock.acquire(0) is False:
return -1
try:
return self.counts.pop(key)
except:
return -1
finally:
lock.release()
counter = Counter()
demo = Bottle()
def get_app():
return demo
@demo.get("/api/<app>/<id>/")
def do_get(app, id):
return counter.get(app, id)
@demo.post("/api/<app>/<id>/")
def do_get(app, id):
return counter.inc(app, id, 1)
@demo.delete("/api/<app>/<id>/")
def do_get(app, id):
result = counter.delete()
if result <= 0:
return False
return True
if __name__ == '__main__':
run(server='gevent', host='192.168.0.131', port=8102, app=demo)