@@ -101,12 +101,22 @@ def server_port(self):
101101 """
102102 return self ._server .server_address [1 ]
103103
104+ def activate (self ):
105+ """Bind port and activate the server but do not yet start serving.
106+
107+ :return Port number that the server is going to use. This is the
108+ actual port to use, even if the initially given port is 0.
109+ """
110+ return self ._server .activate ()
111+
104112 def serve (self , log = True ):
105113 """Start the server and wait for it to be stopped.
106114
107115 :param log: When ``True``, print messages about start and stop to
108116 the console.
109117
118+ Automatically activates the server if it is not activated already.
119+
110120 If this method is executed in the main thread, automatically registers
111121 signals SIGINT, SIGTERM and SIGHUP to stop the server.
112122
@@ -186,23 +196,14 @@ def __init__(self, host, port):
186196 self ._activated = False
187197 self ._stopper_thread = None
188198
189- # TODO: Expose in Remote server itself
190199 def activate (self ):
191- """Activate server but do not yet start serving.
192-
193- Most importantly, port is bind and :attr:`server_address` contains
194- the actual port number to use also if the initially given port is 0.
195- """
196200 if not self ._activated :
197201 self .server_bind ()
198202 self .server_activate ()
199203 self ._activated = True
204+ return self .server_address [1 ]
200205
201206 def serve (self ):
202- """Start serving until the server is stopped.
203-
204- Activates the server automatically if it is not done before.
205- """
206207 self .activate ()
207208 try :
208209 self .serve_forever ()
@@ -213,6 +214,7 @@ def serve(self):
213214 self .server_close ()
214215 if self ._stopper_thread :
215216 self ._stopper_thread .join ()
217+ self ._stopper_thread = None
216218
217219 def stop (self ):
218220 self ._stopper_thread = threading .Thread (target = self .shutdown )
0 commit comments