Lutz Roeder 4 лет назад
Родитель
Сommit
7842269f6d
2 измененных файлов с 20 добавлено и 9 удалено
  1. 1 0
      source/__init__.py
  2. 19 9
      source/server.py

+ 1 - 0
source/__init__.py

@@ -5,6 +5,7 @@ import os
 
 from .server import start
 from .server import stop
+from .server import status
 from .server import wait
 from .server import serve
 from .__version__ import __version__

+ 19 - 9
source/server.py

@@ -161,10 +161,17 @@ def _add_thread(thread):
     global _thread_list
     _thread_list.append(thread)
 
-def _update_thread_list():
+def _update_thread_list(address=None):
     global _thread_list
     _thread_list = [ thread for thread in _thread_list if thread.alive() ]
-    return _thread_list
+    threads = _thread_list
+    if address is not None:
+        address = _make_address(address)
+        if address[1] is None:
+            threads = [ thread for thread in threads if address[0] == thread.address[0] ]
+        else:
+            threads = [ thread for thread in threads if address[0] == thread.address[0] and address[1] == thread.address[1] ]
+    return threads
 
 def _make_address(address):
     if address is None or isinstance(address, int):
@@ -212,17 +219,20 @@ def stop(address=None):
     Args:
         address (tuple, optional): A (host, port) tuple, or a port number.
     '''
-    threads = _update_thread_list()
-    if address is not None:
-        address = _make_address(address)
-        if address[1] is None:
-            threads = [ thread for thread in threads if address[0] == thread.address[0] ]
-        else:
-            threads = [ thread for thread in threads if address[0] == thread.address[0] and address[1] == thread.address[1] ]
+    threads = _update_thread_list(address)
     for thread in threads:
         thread.stop()
     _update_thread_list()
 
+def status(adrress=None):
+    '''Is model served at address.
+
+    Args:
+        address (tuple, optional): A (host, port) tuple, or a port number.
+    '''
+    threads = _update_thread_list(adrress)
+    return len(threads) > 0
+
 def wait():
     '''Wait for console exit and stop all model servers.'''
     try: