working with two ctrl-c, previous commit does not
This commit is contained in:
		
							parent
							
								
									38cd99fa73
								
							
						
					
					
						commit
						e8d04cff7c
					
				| @ -1,6 +1,7 @@ | |||||||
| import argparse | import argparse | ||||||
| import asyncio | import asyncio | ||||||
| import signal | import signal | ||||||
|  | import sys | ||||||
| import threading | import threading | ||||||
| from http.server import HTTPServer, SimpleHTTPRequestHandler | from http.server import HTTPServer, SimpleHTTPRequestHandler | ||||||
| 
 | 
 | ||||||
| @ -9,6 +10,7 @@ import socketio | |||||||
| from .decorator import register_service | from .decorator import register_service | ||||||
| from .get_ip import get_ip_address | from .get_ip import get_ip_address | ||||||
| 
 | 
 | ||||||
|  | http_server_thread = None | ||||||
| 
 | 
 | ||||||
| def start_client(host_ip="0.0.0.0", host_port=4999): | def start_client(host_ip="0.0.0.0", host_port=4999): | ||||||
|     @register_service( |     @register_service( | ||||||
| @ -19,26 +21,37 @@ def start_client(host_ip="0.0.0.0", host_port=4999): | |||||||
|         host_port=host_port, |         host_port=host_port, | ||||||
|     ) |     ) | ||||||
|     def server(port=13373): |     def server(port=13373): | ||||||
|         httpd = HTTPServer(("", port), SimpleHTTPRequestHandler) |         def start_server(): | ||||||
|         print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...") |             global http_server_thread | ||||||
| 
 |             print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...") | ||||||
|         def serve_forever_nonblocking(): |             httpd = HTTPServer(("", port), SimpleHTTPRequestHandler) | ||||||
|             httpd.serve_forever() |             httpd.serve_forever() | ||||||
| 
 |         server_thread = threading.Thread(target=start_server) | ||||||
|         server_thread = threading.Thread(target=serve_forever_nonblocking) |         server_thread.daemon = True | ||||||
|         server_thread.start() |         server_thread.start() | ||||||
|  |         server_thread.join() | ||||||
| 
 | 
 | ||||||
|         return server_thread |     def signal_handler(signal, frame): | ||||||
| 
 |  | ||||||
|     try: |  | ||||||
|         server_thread = server() |  | ||||||
|         while True: |  | ||||||
|             pass |  | ||||||
|     except (KeyboardInterrupt, asyncio.exceptions.CancelledError): |  | ||||||
|         print("Cleaning up and shutting down...") |         print("Cleaning up and shutting down...") | ||||||
|         # server_thread.join() | 
 | ||||||
|  |         # If the HTTP server thread is running, shut it down | ||||||
|  |         if http_server_thread is not None and http_server_thread.is_alive(): | ||||||
|  |             http_server_thread.shutdown() | ||||||
|  | 
 | ||||||
|  |         sys.exit(0) | ||||||
|  | 
 | ||||||
|  |     signal.signal(signal.SIGINT, signal_handler) | ||||||
|  |     signal.signal(signal.SIGTERM, signal_handler) | ||||||
|  |     try: | ||||||
|  |         server() | ||||||
|  |         signal.pause() | ||||||
|  |     except asyncio.exceptions.CancelledError: | ||||||
|  |         print("CancelledError") | ||||||
|  |         # signal_handler(signal.SIGINT, None) | ||||||
|  |         pass | ||||||
|     finally: |     finally: | ||||||
|         print("Shutting down test client...") |         print("Shutting down test client...") | ||||||
|  |         sys.exit(0) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ import socketio | |||||||
| 
 | 
 | ||||||
| sio = socketio.AsyncClient() | sio = socketio.AsyncClient() | ||||||
| 
 | 
 | ||||||
|  | import signal | ||||||
| 
 | 
 | ||||||
| async def _announce_server(**kwargs): | async def _announce_server(**kwargs): | ||||||
|     SERVER_NAME = kwargs.get("name", "server_1") |     SERVER_NAME = kwargs.get("name", "server_1") | ||||||
| @ -68,6 +69,38 @@ def register_service(task=None, **outer_kwargs): | |||||||
| 
 | 
 | ||||||
|     return wrapper |     return wrapper | ||||||
| 
 | 
 | ||||||
|  | # def register_service(task=None, **outer_kwargs): | ||||||
|  | #     if task is None: | ||||||
|  | #         return lambda f: register_service(f, **outer_kwargs) | ||||||
|  | 
 | ||||||
|  | #     @wraps(task) | ||||||
|  | #     def wrapper(*args, **kwargs): | ||||||
|  | #         async def main(*args, **kwargs): | ||||||
|  | #             loop = asyncio.get_event_loop() | ||||||
|  | #             host_block_thread = loop.run_in_executor(None, task) | ||||||
|  | 
 | ||||||
|  | #             # Announce the server to the host | ||||||
|  | #             await _announce_server(**outer_kwargs) | ||||||
|  | 
 | ||||||
|  | #             # Set up signal handlers to clean up properly | ||||||
|  | #             def signal_handler(signum, frame): | ||||||
|  | #                 print(f"Received signal {signum}. Cleaning up and shutting down...") | ||||||
|  | #                 host_block_thread.cancel() | ||||||
|  | #                 import sys | ||||||
|  | #                 sys.exit(0) | ||||||
|  | 
 | ||||||
|  | #             signal.signal(signal.SIGINT, signal_handler) | ||||||
|  | #             signal.signal(signal.SIGTERM, signal_handler) | ||||||
|  | 
 | ||||||
|  | #             # Wait for host_block to finish or to be cancelled | ||||||
|  | #             try: | ||||||
|  | #                 await host_block_thread | ||||||
|  | #             except asyncio.CancelledError: | ||||||
|  | #                 pass | ||||||
|  | 
 | ||||||
|  | #         return asyncio.run(main()) | ||||||
|  | 
 | ||||||
|  | #     return wrapper | ||||||
| 
 | 
 | ||||||
| def announce_server(*args, **kwargs): | def announce_server(*args, **kwargs): | ||||||
|     """Wrapper for register_service""" |     """Wrapper for register_service""" | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user