Sfoglia il codice sorgente

fix: utils.wait_host_port() not compatible below py3.7

wangweimin 4 anni fa
parent
commit
9aa75e71e3
2 ha cambiato i file con 8 aggiunte e 3 eliminazioni
  1. 1 1
      pywebio/platform/tornado.py
  2. 7 2
      pywebio/utils.py

+ 1 - 1
pywebio/platform/tornado.py

@@ -138,7 +138,7 @@ def webio_handler(applications, allowed_origins=None, check_origin=None):
 
 
 async def open_webbrowser_on_server_started(host, port):
 async def open_webbrowser_on_server_started(host, port):
     url = 'http://%s:%s' % (host, port)
     url = 'http://%s:%s' % (host, port)
-    is_open = await wait_host_port(host, port, duration=30, delay=0.5)
+    is_open = await wait_host_port(host, port, duration=20)
     if is_open:
     if is_open:
         logger.info('Try open %s in web browser' % url)
         logger.info('Try open %s in web browser' % url)
         webbrowser.open(url)
         webbrowser.open(url)

+ 7 - 2
pywebio/utils.py

@@ -133,9 +133,14 @@ async def wait_host_port(host, port, duration=10, delay=2):
     tmax = time.time() + duration
     tmax = time.time() + duration
     while time.time() < tmax:
     while time.time() < tmax:
         try:
         try:
-            _reader, writer = await asyncio.wait_for(asyncio.open_connection(host, port), timeout=5)
+            _, writer = await asyncio.wait_for(asyncio.open_connection(host, port), timeout=5)
             writer.close()
             writer.close()
-            await writer.wait_closed()
+
+            # asyncio.StreamWriter.wait_closed is introduced in py 3.7
+            # See https://docs.python.org/3/library/asyncio-stream.html#asyncio.StreamWriter.wait_closed
+            if hasattr(writer, 'wait_closed'):
+                await writer.wait_closed()
+
             return True
             return True
         except Exception:
         except Exception:
             if delay:
             if delay: