-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueManager
More file actions
60 lines (47 loc) · 1.61 KB
/
Copy pathQueueManager
File metadata and controls
60 lines (47 loc) · 1.61 KB
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
from Queue import Queue
import cherrypy
import json
from functools import wraps
#from subprocess import Popen, PIPE
def jsonify(func):
'''JSON decorator for CherryPy'''
@wraps(func)
def wrapper(*args, **kwargs): # *args are the function positional arguments, kwargs are the function keyword arguments
# call the function and store the result in the value variable
value = func(*args, **kwargs)
# set the response content-type to json
cherrypy.response.headers["Content-Type"] = "application/json"
# serialize as json
return json.dumps(value, indent=4, default=lambda o: o.__dict__)
return wrapper
class QueueManager:
exposed=True
def __init__(self):
self.coda=Queue(0)
@jsonify
def POST(self):
add_request=json.loads(cherrypy.request.body.read())
self.coda.put(add_request["pc_id"])
@jsonify
def GET(self, check_is_empty=None, check_length=None):
data={}
if(check_is_empty!=None):
data['empty']=self.coda.empty()
return data
else:
if(check_length!=None):
data['size']=self.coda.qsize()
return data
else:
data['pc_id']=self.coda.get_nowait()
return data
if __name__ == '__main__':
queuemanager=QueueManager()
cherrypy.tree.mount(
queuemanager, '/api/v1/queuemanager',
{'/':
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}
)
cherrypy.engine.start()
cherrypy.engine.block()