Skip to content

Commit

Permalink
Improving support for warm and autoreload with num-procs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Aug 26, 2024
1 parent a4cb22e commit 391fa75
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
10 changes: 6 additions & 4 deletions panel/command/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os
import pathlib
import sys

from glob import glob
from types import ModuleType
Expand Down Expand Up @@ -280,11 +281,11 @@ def customize_applications(self, args, applications):
applications['/'] = applications[f'/{index}']
return super().customize_applications(args, applications)

def warm_applications(self, applications, reuse_sessions, error=True):
def warm_applications(self, applications, reuse_sessions, error=True, initialize_session=True):
from ..io.session import generate_session
for path, app in applications.items():
try:
session = generate_session(app)
session = generate_session(app, initialize=initialize_session)
except Exception as e:
if error:
raise e
Expand Down Expand Up @@ -366,13 +367,14 @@ def customize_kwargs(self, args, server_kwargs):
if args.warm or args.autoreload:
argvs = {f: args.args for f in files}
applications = build_single_handler_applications(files, argvs)
initialize_session = not (args.num_procs and sys.version_info < (3, 12))
if args.autoreload:
with record_modules(list(applications.values())):
self.warm_applications(
applications, args.reuse_sessions, error=False
applications, args.reuse_sessions, error=False, initialize_session=initialize_session
)
else:
self.warm_applications(applications, args.reuse_sessions)
self.warm_applications(applications, args.reuse_sessions, initialize_session=initialize_session)

if args.liveness:
argvs = {f: args.args for f in files}
Expand Down
35 changes: 35 additions & 0 deletions panel/tests/command/test_serve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import tempfile

import pytest
Expand Down Expand Up @@ -103,3 +104,37 @@ def test_serve_markdown():
r = requests.get(f"http://localhost:{port}/")
assert r.status_code == 200
assert '<title>My app</title>' in r.content.decode('utf-8')


@linux_only
@pytest.mark.parametrize("arg", ["--warm", "--autoreload"])
def test_serve_num_procs(arg):
app = "import panel as pn; pn.panel('Hello').servable()"
py = tempfile.NamedTemporaryFile(mode='w', suffix='.py')
write_file(app, py.file)

regex = re.compile(r'Starting Bokeh server with process id: (\d+)')
with run_panel_serve(["--port", "0", py.name, "--num-procs", "2", arg]) as p:
pid1 = wait_for_port(p.stdout, regex=regex)
pid2 = wait_for_port(p.stdout, regex=regex)

Check failure on line 119 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / core:test-core:ubuntu-latest

test_serve_num_procs[--warm] Failed: No matching log line in process output, following output was captured:

Check failure on line 119 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / core:test-core:ubuntu-latest

test_serve_num_procs[--autoreload] Failed: No matching log line in process output, following output was captured:

Check failure on line 119 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / unit:test-310:ubuntu-latest

test_serve_num_procs[--autoreload] Failed: No matching log line in process output, following output was captured:

Check failure on line 119 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / unit:test-310:ubuntu-latest

test_serve_num_procs[--warm] Failed: No matching log line in process output, following output was captured:

Check failure on line 119 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / unit:test-312:ubuntu-latest

test_serve_num_procs[--autoreload] Failed: No matching log line in process output, following output was captured:
assert pid1 != pid2
#
@linux_only
def test_serve_num_procs_setup():
app = "import panel as pn; pn.panel('Hello').servable()"
py = tempfile.NamedTemporaryFile(mode='w', suffix='.py')
write_file(app, py.file)

setup_app = """\
import os
import time
time.sleep(1)
print(f"Setup PID {os.getpid()}", flush=True)"""
setup_py = tempfile.NamedTemporaryFile(mode='w', suffix='.py')
write_file(setup_app, setup_py.file)

regex = re.compile(r'Setup PID (\d+)')
with run_panel_serve(["--port", "0", py.name, "--num-procs", "2", "--setup", setup_py.name]) as p:
pid1 = wait_for_port(p.stdout, regex=regex)
pid2 = wait_for_port(p.stdout, regex=regex)

Check failure on line 139 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / core:test-core:ubuntu-latest

test_serve_num_procs_setup Failed: No matching log line in process output, following output was captured:

Check failure on line 139 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / unit:test-310:ubuntu-latest

test_serve_num_procs_setup Failed: No matching log line in process output, following output was captured:

Check failure on line 139 in panel/tests/command/test_serve.py

View workflow job for this annotation

GitHub Actions / unit:test-312:ubuntu-latest

test_serve_num_procs_setup Failed: No matching log line in process output, following output was captured:
assert pid1 != pid2
4 changes: 2 additions & 2 deletions panel/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def readline(self, timeout=None):
except Empty:
return None

def wait_for_port(stdout):
def wait_for_port(stdout, regex=APP_PATTERN):
nbsr = NBSR(stdout)
m = None
output = []
Expand All @@ -381,7 +381,7 @@ def wait_for_port(stdout):
continue
out = o.decode('utf-8')
output.append(out)
m = APP_PATTERN.search(out)
m = regex.search(out)
if m is not None:
break
if m is None:
Expand Down

0 comments on commit 391fa75

Please sign in to comment.