Browse Source

refine manyuser thread start & stop

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

24
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,6 +166,7 @@ class DbTransfer(object):
timeout = 60 timeout = 60
socket.setdefaulttimeout(timeout) socket.setdefaulttimeout(timeout)
last_rows = [] last_rows = []
try:
while True: while True:
try: try:
DbTransfer.get_instance().push_db_all_user() DbTransfer.get_instance().push_db_all_user()
@ -165,6 +177,14 @@ class DbTransfer(object):
trace = traceback.format_exc() trace = traceback.format_exc()
logging.error(trace) logging.error(trace)
#logging.warn('db thread except:%s' % e) #logging.warn('db thread except:%s' % e)
finally: if DbTransfer.get_instance().event.wait(15):
time.sleep(15) break
except KeyboardInterrupt as e:
pass
DbTransfer.del_servers()
ServerPool.get_instance().stop()
@staticmethod
def thread_db_stop():
DbTransfer.get_instance().event.set()

10
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()
try:
while True: while True:
time.sleep(99999) 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