From b8fd373dd2df6c286a794f3ceb7b7375cc4e23cd Mon Sep 17 00:00:00 2001 From: Anand Tripathi Date: Sun, 15 May 2022 16:10:59 +0200 Subject: [PATCH] #183 port open issue (#196) --- robyn/__init__.py | 9 +++++++++ robyn/processpool.py | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/robyn/__init__.py b/robyn/__init__.py index ff760a150..8c623cf5f 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -104,6 +104,7 @@ def start(self, url="127.0.0.1", port=5000): """ if not self.dev: + processes = [] workers = self.workers socket = SocketHeld(url, port) for _ in range(self.processes): @@ -122,8 +123,16 @@ def start(self, url="127.0.0.1", port=5000): ), ) p.start() + processes.append(p) print("Press Ctrl + C to stop \n") + try: + for process in processes: + process.join() + except KeyboardInterrupt: + print(f"\n{Colors.BOLD}{Colors.OKGREEN} Terminating server!! {Colors.ENDC}") + for process in processes: + process.kill() else: event_handler = EventHandler(self.file_path) event_handler.start_server_first_time() diff --git a/robyn/processpool.py b/robyn/processpool.py index cd457226c..a1330c42e 100644 --- a/robyn/processpool.py +++ b/robyn/processpool.py @@ -75,5 +75,9 @@ def spawn_process( web_socket.methods["message"], ) - server.start(socket, workers) - asyncio.get_event_loop().run_forever() + try: + server.start(socket, workers) + loop = asyncio.get_event_loop() + loop.run_forever() + except KeyboardInterrupt: + loop.close()