Browse Source

refine manyuser thread start & stop

dev
BreakWa11 9 years ago
parent
commit
90ae1fdc40
  1. 44
      db_transfer.py
  2. 14
      server.py
  3. 3
      server_pool.py

44
db_transfer.py

@ -15,7 +15,9 @@ class DbTransfer(object):
instance = None instance = None
def __init__(self): def __init__(self):
import threading
self.last_get_transfer = {} self.last_get_transfer = {}
self.event = threading.Event()
@staticmethod @staticmethod
def get_instance(): def get_instance():
@ -148,6 +150,15 @@ class DbTransfer(object):
logging.info('db stop server at port [%s] reason: port not exist' % (row['port'])) logging.info('db stop server at port [%s] reason: port not exist' % (row['port']))
ServerPool.get_instance().cb_del_server(row['port']) ServerPool.get_instance().cb_del_server(row['port'])
@staticmethod
def del_servers():
for port in ServerPool.get_instance().tcp_servers_pool.keys():
if ServerPool.get_instance().server_is_run(port) > 0:
ServerPool.get_instance().cb_del_server(port)
for port in ServerPool.get_instance().tcp_ipv6_servers_pool.keys():
if ServerPool.get_instance().server_is_run(port) > 0:
ServerPool.get_instance().cb_del_server(port)
@staticmethod @staticmethod
def thread_db(): def thread_db():
import socket import socket
@ -155,16 +166,25 @@ class DbTransfer(object):
timeout = 60 timeout = 60
socket.setdefaulttimeout(timeout) socket.setdefaulttimeout(timeout)
last_rows = [] last_rows = []
while True: try:
try: while True:
DbTransfer.get_instance().push_db_all_user() try:
rows = DbTransfer.get_instance().pull_db_all_user() DbTransfer.get_instance().push_db_all_user()
DbTransfer.del_server_out_of_bound_safe(last_rows, rows) rows = DbTransfer.get_instance().pull_db_all_user()
last_rows = rows DbTransfer.del_server_out_of_bound_safe(last_rows, rows)
except Exception as e: last_rows = rows
trace = traceback.format_exc() except Exception as e:
logging.error(trace) trace = traceback.format_exc()
#logging.warn('db thread except:%s' % e) logging.error(trace)
finally: #logging.warn('db thread except:%s' % e)
time.sleep(15) if DbTransfer.get_instance().event.wait(15):
break
except KeyboardInterrupt as e:
pass
DbTransfer.del_servers()
ServerPool.get_instance().stop()
@staticmethod
def thread_db_stop():
DbTransfer.get_instance().event.set()

14
server.py

@ -39,15 +39,23 @@ class MainThread(threading.Thread):
def run(self): def run(self):
db_transfer.DbTransfer.thread_db() db_transfer.DbTransfer.thread_db()
def stop(self):
db_transfer.DbTransfer.thread_db_stop()
def main(): def main():
shell.check_python() shell.check_python()
if True: if False:
db_transfer.DbTransfer.thread_db() db_transfer.DbTransfer.thread_db()
else: else:
thread = MainThread() thread = MainThread()
thread.start() thread.start()
while True: try:
time.sleep(99999) while True:
time.sleep(99999)
except (KeyboardInterrupt, IOError, OSError) as e:
import traceback
traceback.print_exc()
thread.stop()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

3
server_pool.py

@ -72,6 +72,9 @@ class ServerPool(object):
ServerPool.instance = ServerPool() ServerPool.instance = ServerPool()
return ServerPool.instance return ServerPool.instance
def stop(self):
self.loop.stop()
@staticmethod @staticmethod
def _loop(loop, dns_resolver, mgr): def _loop(loop, dns_resolver, mgr):
try: try:

Loading…
Cancel
Save