Skip to content

Commit

Permalink
fix: utils.wait_host_port() not compatible below py3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed Feb 3, 2021
1 parent 81a88db commit 9aa75e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pywebio/platform/tornado.py
Expand Up @@ -138,7 +138,7 @@ def webio_handler(applications, allowed_origins=None, check_origin=None):

async def open_webbrowser_on_server_started(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:
logger.info('Try open %s in web browser' % url)
webbrowser.open(url)
Expand Down
9 changes: 7 additions & 2 deletions pywebio/utils.py
Expand Up @@ -133,9 +133,14 @@ async def wait_host_port(host, port, duration=10, delay=2):
tmax = time.time() + duration
while time.time() < tmax:
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()
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
except Exception:
if delay:
Expand Down

0 comments on commit 9aa75e7

Please sign in to comment.