switch to multiprocessing, attempt to improve KeyboardInterrupt handling (or add it, actually)

This commit is contained in:
Xevion
2021-01-21 11:59:11 -06:00
parent 7185d1d1ae
commit 50ddf83ed7
2 changed files with 19 additions and 10 deletions

View File

@@ -0,0 +1,4 @@
import logging
logging.basicConfig(level=logging.DEBUG,
format='[%(asctime)s] [%(levelname)s] [%(threadName)s] %(message)s')

View File

@@ -1,6 +1,6 @@
import logging import logging
import multiprocessing
import socket import socket
import threading
from server import handler from server import handler
@@ -21,17 +21,22 @@ clients = []
# Receiving / Listening Function # Receiving / Listening Function
def receive(): def receive():
while True: while True:
# Accept Connection try:
conn, address = server.accept() # Accept Connection
logger.info(f"New connection from {address}") conn, address = server.accept()
logger.info(f"New connection from {address}")
client = handler.Client(conn, address, clients) client = handler.Client(conn, address, clients)
clients.append(client) clients.append(client)
client.request_nickname() client.request_nickname()
# Start Handling Thread For Client # Start Handling Thread For Client
thread = threading.Thread(target=client.handle, name=client.id[:8]) thread = multiprocessing.Process(target=client.handle, name=client.id[:8])
thread.start() thread.start()
except KeyboardInterrupt:
logger.info('Server closed by user.')
except Exception as e:
logger.critical(e, exc_info=e)
if __name__ == '__main__': if __name__ == '__main__':