mirror of
https://github.com/Xevion/tcp-chat.git
synced 2025-12-06 05:16:45 -06:00
switch to multiprocessing, attempt to improve KeyboardInterrupt handling (or add it, actually)
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG,
|
||||||
|
format='[%(asctime)s] [%(levelname)s] [%(threadName)s] %(message)s')
|
||||||
|
|||||||
@@ -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__':
|
||||||
|
|||||||
Reference in New Issue
Block a user