あなた:
PythonでKeyboardInterruptの例外処理しようとしても、どの例外もつかめずにエラーになります。コンソール上には、KeyboardInterruptの表示が3つほど出ており、二つはマルチプロセッシングの分だと思うのですが、もう一つのこのコード自体がのKeyboardInterruptがつかめないのがよくわかりません。
py
# main server scirpt
# serve dataserver function with websocket.
import asyncio
import multiprocessing
# from modules.reqestParser import parser
from websockets.asyncio.server import serve
from helper import loadSettings
from helper.netwrok import receiveLoop
import tasks
import controller
import sys
websocketRef = None
async def mainLoop(websocket):
websocketRef = websocket
await receiveLoop(websocket,controller.controller)
async def dataserver():
async with serve(mainLoop, loadSettings.settings["ip"], loadSettings.settings["port"]) as server:
await server.serve_forever()
def dataserverThread():
asyncio.run(dataserver())
def serviceThread():
asyncio.run(tasks.taskController(loadSettings.settings["taskInterval"]))
try:
if __name__ == "__main__":
print("This dataserver is started.")
multiprocessing.Process(target=dataserverThread).start()
multiprocessing.Process(target=serviceThread).start()
except KeyboardInterrupt:
print("This DataServer will be stopped.")
# if(websocketRef != None):
# websocketRef.close()
# else:
# print("The websocket connection could be not closed correctly.")
sys.exit()