winlin

add threading.Lock for cherrypy, or performance suffer.

... ... @@ -368,7 +368,8 @@ class RESTServers(object):
self.__nodes = []
self.__last_update = datetime.datetime.now();
server_ip = "192.168.1.142";
self.__lock = threading.Lock()
def __get_node(self, device_id):
for node in self.__nodes:
... ... @@ -416,6 +417,9 @@ class RESTServers(object):
def POST(self):
enable_crossdomain()
try:
self.__lock.acquire()
req = cherrypy.request.body.read()
trace("post to nodes, req=%s"%(req))
try:
... ... @@ -437,6 +441,8 @@ class RESTServers(object):
node.heartbeat = time.time()
return json.dumps({"code":Error.success, "data": {"id":node.id}})
finally:
self.__lock.release()
'''
id canbe:
... ... @@ -455,6 +461,9 @@ class RESTServers(object):
def GET(self, id=None, action="play", stream="live/livestream", index=None, local="false", device_id=None):
enable_crossdomain()
try:
self.__lock.acquire()
self.__refresh_nodes()
data = self.__json_dump_nodes(self.__nodes)
... ... @@ -507,6 +516,8 @@ class RESTServers(object):
return json.dumps(data)
#return "id=%s, action=%s, stream=%s, url=%s, index=%s, local=%s"%(id, action, stream, url, index, local)
raise cherrypy.HTTPRedirect(url)
finally:
self.__lock.release()
def DELETE(self, id):
enable_crossdomain()
... ... @@ -577,6 +588,10 @@ class RESTNodes(object):
def __init__(self):
self.__nodes = []
# @remark, if there is shared data, such as the self.__nodes,
# we must use lock for cherrypy, or the cpu of cherrypy will high
# and performance suffer.
self.__lock = threading.Lock()
def __get_node(self, id):
for node in self.__nodes:
... ... @@ -629,6 +644,9 @@ class RESTNodes(object):
def GET(self, type=None, format=None, origin=None, vhost=None, port=None, stream=None, node_id=None):
enable_crossdomain()
try:
self.__lock.acquire()
self.__refresh_nodes()
data = self.__json_dump_nodes(self.__nodes)
... ... @@ -671,10 +689,15 @@ class RESTNodes(object):
}})
return json.dumps({"code":Error.success, "data": data})
finally:
self.__lock.release()
def PUT(self):
enable_crossdomain()
try:
self.__lock.acquire()
req = cherrypy.request.body.read()
trace("put to nodes, req=%s"%(req))
try:
... ... @@ -708,10 +731,15 @@ class RESTNodes(object):
res = json.dumps({"code":Error.success, "data": {"id":node.id, "peers":peers_data}})
trace(res)
return res
finally:
self.__lock.release()
def POST(self):
enable_crossdomain()
try:
self.__lock.acquire()
req = cherrypy.request.body.read()
trace("post to nodes, req=%s"%(req))
try:
... ... @@ -736,6 +764,8 @@ class RESTNodes(object):
res = json.dumps({"code":Error.success, "data": {"id":node.id, "peers":peers_data}})
trace(res)
return res
finally:
self.__lock.release()
def OPTIONS(self, *args, **kwargs):
enable_crossdomain()
... ... @@ -966,7 +996,8 @@ conf = {
'server.socket_port': port,
'tools.encode.on': True,
'tools.staticdir.on': True,
'tools.encode.encoding': "utf-8"
'tools.encode.encoding': "utf-8",
'server.thread_pool': 2, # single thread server.
},
'/': {
'tools.staticdir.dir': static_dir,
... ...