Skip to content

Commit d6c7d4b

Browse files
authored
Fix wait_for_request (#1125)
1 parent c51c88b commit d6c7d4b

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

docs/source/customize.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ In preheating kernel mode, users can prepend with ``wait_for_request`` from ``vo
410410
411411
``wait_for_request`` will pause the execution of the notebook in the preheated kernel at this cell and wait for an actual user to connect to Voilà, set the request info environment variables and then continue the execution of the remaining cells.
412412

413-
If the Voilà websocket handler is not started with the default protocol (`ws`), the default IP address (`127.0.0.1`) or the default port (`8866`), users need to provide these values through the environment variables ``VOILA_APP_PROTOCOL``, ``VOILA_APP_IP`` and ``VOILA_APP_PORT``. The easiest way is to set these variables in the `voila.json` configuration file, for example:
413+
If the Voilà websocket handler is not started with the default protocol (`ws`), the default IP address (`127.0.0.1`) the default port (`8866`) or with url suffix, users need to provide these values through the environment variables ``VOILA_WS_PROTOCOL``, ``VOILA_APP_IP``, ``VOILA_APP_PORT`` and ``VOILA_WS_BASE_URL``. The easiest way is to set these variables in the `voila.json` configuration file, for example:
414414

415415
.. code-block:: python
416416
@@ -423,7 +423,7 @@ If the Voilà websocket handler is not started with the default protocol (`ws`),
423423
"kernel_env_variables": {
424424
"VOILA_APP_IP": "192.168.1.1",
425425
"VOILA_APP_PORT": "6789",
426-
"VOILA_APP_PROTOCOL": "wss"
426+
"VOILA_WS_PROTOCOL": "wss"
427427
}
428428
}
429429
},

voila/utils.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import json
1212
import os
1313
import threading
14+
import warnings
1415
from enum import Enum
1516
from functools import partial
1617
from typing import Awaitable, Dict
@@ -30,7 +31,8 @@ class ENV_VARIABLE(str, Enum):
3031
VOILA_BASE_URL = 'VOILA_BASE_URL'
3132
VOILA_APP_IP = 'VOILA_APP_IP'
3233
VOILA_APP_PORT = 'VOILA_APP_PORT'
33-
VOILA_APP_PROTOCOL = 'VOILA_APP_PROTOCOL'
34+
VOILA_WS_PROTOCOL = 'VOILA_WS_PROTOCOL'
35+
VOILA_WS_BASE_URL = 'VOILA_WS_BASE_URL'
3436
SERVER_NAME = 'SERVER_NAME'
3537
SERVER_PORT = 'SERVER_PORT'
3638
SCRIPT_NAME = 'SCRIPT_NAME'
@@ -57,9 +59,14 @@ def get_server_root_dir(settings):
5759

5860

5961
async def _get_request_info(ws_url: str) -> Awaitable:
60-
async with websockets.connect(ws_url) as websocket:
61-
ri = await websocket.recv()
62-
return ri
62+
try:
63+
async with websockets.connect(ws_url, open_timeout=5) as websocket:
64+
ri = await websocket.recv()
65+
except (TimeoutError, ConnectionRefusedError):
66+
warnings.warn(f'Failed to connect to {ws_url}')
67+
return None
68+
else:
69+
return ri
6370

6471

6572
def wait_for_request(url: str = None) -> str:
@@ -73,17 +80,16 @@ def wait_for_request(url: str = None) -> str:
7380
Defaults to None.
7481
7582
"""
76-
7783
preheat_mode = os.getenv(ENV_VARIABLE.VOILA_PREHEAT, 'False')
7884
if preheat_mode == 'False':
7985
return
8086

8187
request_info = None
8288
if url is None:
83-
protocol = os.getenv(ENV_VARIABLE.VOILA_APP_PROTOCOL, 'ws')
89+
protocol = os.getenv(ENV_VARIABLE.VOILA_WS_PROTOCOL, 'ws')
8490
server_ip = os.getenv(ENV_VARIABLE.VOILA_APP_IP, '127.0.0.1')
8591
server_port = os.getenv(ENV_VARIABLE.VOILA_APP_PORT, '8866')
86-
base_url = os.getenv(ENV_VARIABLE.VOILA_BASE_URL, '/')
92+
base_url = os.getenv(ENV_VARIABLE.VOILA_WS_BASE_URL, '/')
8793
url = f'{protocol}://{server_ip}:{server_port}{base_url}voila/query'
8894

8995
kernel_id = os.getenv(ENV_VARIABLE.VOILA_KERNEL_ID)
@@ -101,8 +107,9 @@ def inner():
101107
except (KeyboardInterrupt, SystemExit):
102108
asyncio.get_event_loop().stop()
103109

104-
for k, v in json.loads(request_info).items():
105-
os.environ[k] = v
110+
if request_info is not None:
111+
for k, v in json.loads(request_info).items():
112+
os.environ[k] = v
106113

107114

108115
def get_query_string(url: str = None) -> str:

voila/voila_kernel_manager.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,16 @@ def fill_if_needed(
175175
except RuntimeError:
176176
loop = asyncio.new_event_loop()
177177
asyncio.set_event_loop(loop)
178-
if notebook_name in self.kernel_pools_config:
179-
kernel_size = self.kernel_pools_config[notebook_name].get(
180-
'pool_size', 1
181-
)
182-
else:
183-
default_config = self.kernel_pools_config.get('default', {})
184-
kernel_size = default_config.get('pool_size', 1)
178+
default_config: dict = self.kernel_pools_config.get('default', {})
179+
notebook_config: dict = self.kernel_pools_config.get(
180+
notebook_name, default_config
181+
)
182+
kernel_env_variables: dict = notebook_config.get(
183+
'kernel_env_variables', default_config.get('kernel_env_variables', {})
184+
)
185+
kernel_size: int = notebook_config.get(
186+
'pool_size', default_config.get('pool_size', 1)
187+
)
185188
pool = self._pools.get(notebook_name, [])
186189
self._pools[notebook_name] = pool
187190
if 'path' not in kwargs:
@@ -193,7 +196,7 @@ def fill_if_needed(
193196
kernel_env = os.environ.copy()
194197
kernel_env_arg = kwargs.get('env', {})
195198
kernel_env.update(kernel_env_arg)
196-
kernel_env_variables = self.kernel_pools_config.get(notebook_name, {}).get('kernel_env_variables', {})
199+
197200
for key in kernel_env_variables:
198201
if key not in kernel_env:
199202
kernel_env[key] = kernel_env_variables[key]

0 commit comments

Comments
 (0)