1+ import argparse
2+ import logging
3+ import os
4+ import sys
5+
6+ # Configure logging to output to console
7+ logging .basicConfig (
8+ level = logging .INFO ,
9+ format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
10+ )
11+ logger = logging .getLogger (__name__ )
12+
13+
14+ def run_pyro_nameserver ():
15+ """
16+ Run the Pyro4 nameserver directly in the terminal.
17+ This allows stopping the server with Ctrl+C.
18+ """
19+ parser = argparse .ArgumentParser (
20+ description = "Start Pyro4 nameserver for QICK integration. "
21+ "The nameserver runs in the foreground and can be stopped with Ctrl+C."
22+ )
23+ parser .add_argument (
24+ '--host' ,
25+ '-n' ,
26+ default = 'localhost' ,
27+ help = 'Host address for the nameserver (default: localhost)'
28+ )
29+ parser .add_argument (
30+ '--port' ,
31+ '-p' ,
32+ type = int ,
33+ default = 8888 ,
34+ help = 'Port number for the nameserver (default: 8888)'
35+ )
36+
37+ args = parser .parse_args ()
38+
39+ # Set required environment variables for Pyro4
40+ os .environ ["PYRO_SERIALIZERS_ACCEPTED" ] = "pickle"
41+ os .environ ["PYRO_PICKLE_PROTOCOL_VERSION" ] = "4"
42+
43+ logger .info (f"Starting Pyro4 nameserver on { args .host } :{ args .port } " )
44+ logger .info ("Press Ctrl+C to stop the nameserver" )
45+
46+ # Build the command to execute pyro4-ns
47+ cmd = [
48+ "pyro4-ns" ,
49+ "-n" ,
50+ args .host ,
51+ "-p" ,
52+ str (args .port ),
53+ ]
54+
55+ try :
56+ # Replace the current process with pyro4-ns
57+ # This will run pyro4-ns directly in the terminal
58+ os .execvp ("pyro4-ns" , cmd )
59+ except FileNotFoundError :
60+ logger .error ("pyro4-ns command not found. Please install Pyro4 with: pip install Pyro4" )
61+ sys .exit (1 )
62+ except Exception as e :
63+ logger .error (f"Failed to start Pyro4 nameserver: { e } " )
64+ logger .error (f"Error type: { type (e ).__name__ } " )
65+ import traceback
66+ traceback .print_exc ()
67+ sys .exit (1 )
68+
69+
70+ if __name__ == "__main__" :
71+ run_pyro_nameserver ()
0 commit comments