Skip to content

Commit a60f7ee

Browse files
committed
Add --reuse-sessions warm option (#8087)
* Add --reuse-sessions warm option * Revert default
1 parent 2564659 commit a60f7ee

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

doc/how_to/performance/reuse_sessions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ panel serve app.py --reuse-sessions --global-loading-spinner
1616

1717
:::{tip}
1818
If you want to warm up the server with the cached sessions you can also provide the `--warm` option.
19+
20+
Optionally you can also configure `--reuse-sessions warm`, which will warm the session even before the Websocket request requesting it arrives.
1921
:::
2022

2123
## More complex cases

panel/command/serve.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,11 @@ class Serve(_BkServe):
303303
type = str
304304
)),
305305
('--reuse-sessions', Argument(
306-
action = 'store_true',
306+
action = 'store',
307307
help = "Whether to reuse sessions when serving the initial request.",
308+
default = False,
309+
const = True,
310+
nargs = "?"
308311
)),
309312
('--global-loading-spinner', Argument(
310313
action = 'store_true',

panel/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,15 @@ class _config(_base_config):
189189
The notification to display when the application is ready and
190190
fully loaded.""")
191191

192-
reuse_sessions = param.Boolean(default=False, doc="""
192+
reuse_sessions = param.Selector(default=False, objects=[True, False, 'warm'], doc="""
193193
Whether to reuse a session for the initial request to speed up
194194
the initial page render. Note that if the initial page differs
195195
between sessions, e.g. because it uses query parameters to modify
196196
the rendered content, then this option will result in the wrong
197197
content being rendered. Define a session_key_func to ensure that
198-
reused sessions are only reused when appropriate.""")
198+
reused sessions are only reused when appropriate. If set to 'warm'
199+
session reuse is enabled and the session is warmed up as soon as
200+
the initial request arrives.""")
199201

200202
session_key_func = param.Callable(default=None, doc="""
201203
Used in conjunction with the reuse_sessions option, the

panel/io/server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,15 @@ async def get(self, *args, **kwargs):
516516
expiration=app.session_token_expiration,
517517
extra_payload=extra_payload
518518
)
519+
if config.reuse_sessions == 'warm':
520+
state.execute(
521+
partial(
522+
self.application_context.create_session_if_needed,
523+
session_id,
524+
self.request,
525+
token
526+
)
527+
)
519528
else:
520529
token = session.token
521530
logger.info(LOG_SESSION_CREATED, id(session.document))

0 commit comments

Comments
 (0)