winlin

add threading.Lock for cherrypy, or performance suffer.

@@ -368,7 +368,8 @@ class RESTServers(object): @@ -368,7 +368,8 @@ class RESTServers(object):
368 self.__nodes = [] 368 self.__nodes = []
369 369
370 self.__last_update = datetime.datetime.now(); 370 self.__last_update = datetime.datetime.now();
371 - server_ip = "192.168.1.142"; 371 +
  372 + self.__lock = threading.Lock()
372 373
373 def __get_node(self, device_id): 374 def __get_node(self, device_id):
374 for node in self.__nodes: 375 for node in self.__nodes:
@@ -416,6 +417,9 @@ class RESTServers(object): @@ -416,6 +417,9 @@ class RESTServers(object):
416 def POST(self): 417 def POST(self):
417 enable_crossdomain() 418 enable_crossdomain()
418 419
  420 + try:
  421 + self.__lock.acquire()
  422 +
419 req = cherrypy.request.body.read() 423 req = cherrypy.request.body.read()
420 trace("post to nodes, req=%s"%(req)) 424 trace("post to nodes, req=%s"%(req))
421 try: 425 try:
@@ -437,6 +441,8 @@ class RESTServers(object): @@ -437,6 +441,8 @@ class RESTServers(object):
437 node.heartbeat = time.time() 441 node.heartbeat = time.time()
438 442
439 return json.dumps({"code":Error.success, "data": {"id":node.id}}) 443 return json.dumps({"code":Error.success, "data": {"id":node.id}})
  444 + finally:
  445 + self.__lock.release()
440 446
441 ''' 447 '''
442 id canbe: 448 id canbe:
@@ -455,6 +461,9 @@ class RESTServers(object): @@ -455,6 +461,9 @@ class RESTServers(object):
455 def GET(self, id=None, action="play", stream="live/livestream", index=None, local="false", device_id=None): 461 def GET(self, id=None, action="play", stream="live/livestream", index=None, local="false", device_id=None):
456 enable_crossdomain() 462 enable_crossdomain()
457 463
  464 + try:
  465 + self.__lock.acquire()
  466 +
458 self.__refresh_nodes() 467 self.__refresh_nodes()
459 data = self.__json_dump_nodes(self.__nodes) 468 data = self.__json_dump_nodes(self.__nodes)
460 469
@@ -507,6 +516,8 @@ class RESTServers(object): @@ -507,6 +516,8 @@ class RESTServers(object):
507 return json.dumps(data) 516 return json.dumps(data)
508 #return "id=%s, action=%s, stream=%s, url=%s, index=%s, local=%s"%(id, action, stream, url, index, local) 517 #return "id=%s, action=%s, stream=%s, url=%s, index=%s, local=%s"%(id, action, stream, url, index, local)
509 raise cherrypy.HTTPRedirect(url) 518 raise cherrypy.HTTPRedirect(url)
  519 + finally:
  520 + self.__lock.release()
510 521
511 def DELETE(self, id): 522 def DELETE(self, id):
512 enable_crossdomain() 523 enable_crossdomain()
@@ -577,6 +588,10 @@ class RESTNodes(object): @@ -577,6 +588,10 @@ class RESTNodes(object):
577 588
578 def __init__(self): 589 def __init__(self):
579 self.__nodes = [] 590 self.__nodes = []
  591 + # @remark, if there is shared data, such as the self.__nodes,
  592 + # we must use lock for cherrypy, or the cpu of cherrypy will high
  593 + # and performance suffer.
  594 + self.__lock = threading.Lock()
580 595
581 def __get_node(self, id): 596 def __get_node(self, id):
582 for node in self.__nodes: 597 for node in self.__nodes:
@@ -629,6 +644,9 @@ class RESTNodes(object): @@ -629,6 +644,9 @@ class RESTNodes(object):
629 def GET(self, type=None, format=None, origin=None, vhost=None, port=None, stream=None, node_id=None): 644 def GET(self, type=None, format=None, origin=None, vhost=None, port=None, stream=None, node_id=None):
630 enable_crossdomain() 645 enable_crossdomain()
631 646
  647 + try:
  648 + self.__lock.acquire()
  649 +
632 self.__refresh_nodes() 650 self.__refresh_nodes()
633 data = self.__json_dump_nodes(self.__nodes) 651 data = self.__json_dump_nodes(self.__nodes)
634 652
@@ -671,10 +689,15 @@ class RESTNodes(object): @@ -671,10 +689,15 @@ class RESTNodes(object):
671 }}) 689 }})
672 690
673 return json.dumps({"code":Error.success, "data": data}) 691 return json.dumps({"code":Error.success, "data": data})
  692 + finally:
  693 + self.__lock.release()
674 694
675 def PUT(self): 695 def PUT(self):
676 enable_crossdomain() 696 enable_crossdomain()
677 697
  698 + try:
  699 + self.__lock.acquire()
  700 +
678 req = cherrypy.request.body.read() 701 req = cherrypy.request.body.read()
679 trace("put to nodes, req=%s"%(req)) 702 trace("put to nodes, req=%s"%(req))
680 try: 703 try:
@@ -708,10 +731,15 @@ class RESTNodes(object): @@ -708,10 +731,15 @@ class RESTNodes(object):
708 res = json.dumps({"code":Error.success, "data": {"id":node.id, "peers":peers_data}}) 731 res = json.dumps({"code":Error.success, "data": {"id":node.id, "peers":peers_data}})
709 trace(res) 732 trace(res)
710 return res 733 return res
  734 + finally:
  735 + self.__lock.release()
711 736
712 def POST(self): 737 def POST(self):
713 enable_crossdomain() 738 enable_crossdomain()
714 739
  740 + try:
  741 + self.__lock.acquire()
  742 +
715 req = cherrypy.request.body.read() 743 req = cherrypy.request.body.read()
716 trace("post to nodes, req=%s"%(req)) 744 trace("post to nodes, req=%s"%(req))
717 try: 745 try:
@@ -736,6 +764,8 @@ class RESTNodes(object): @@ -736,6 +764,8 @@ class RESTNodes(object):
736 res = json.dumps({"code":Error.success, "data": {"id":node.id, "peers":peers_data}}) 764 res = json.dumps({"code":Error.success, "data": {"id":node.id, "peers":peers_data}})
737 trace(res) 765 trace(res)
738 return res 766 return res
  767 + finally:
  768 + self.__lock.release()
739 769
740 def OPTIONS(self, *args, **kwargs): 770 def OPTIONS(self, *args, **kwargs):
741 enable_crossdomain() 771 enable_crossdomain()
@@ -966,7 +996,8 @@ conf = { @@ -966,7 +996,8 @@ conf = {
966 'server.socket_port': port, 996 'server.socket_port': port,
967 'tools.encode.on': True, 997 'tools.encode.on': True,
968 'tools.staticdir.on': True, 998 'tools.staticdir.on': True,
969 - 'tools.encode.encoding': "utf-8" 999 + 'tools.encode.encoding': "utf-8",
  1000 + 'server.thread_pool': 2, # single thread server.
970 }, 1001 },
971 '/': { 1002 '/': {
972 'tools.staticdir.dir': static_dir, 1003 'tools.staticdir.dir': static_dir,