Skip to content

Commit 582127f

Browse files
authored
Andrew/issue 383 fix hang (#384)
* Readd missing argument * Add quiet start/stop with automatic stop atexit. * Add new functions to init. * Add silence_warnings argument to sync_server * Remove old safe API * Revert "Remove old safe API" This reverts commit d9f8504. * Fix bad API/docs * Move __all__ to top * Accept kwarg silence_warnings explicitly in sig
1 parent 70f6c31 commit 582127f

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

src/py/kaleido/__init__.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@
1212
from ._page_generator import PageGenerator
1313
from .kaleido import Kaleido
1414

15-
_global_server = _sync_server.GlobalKaleidoServer()
16-
17-
18-
def start_sync_server(*args, **kwargs):
19-
"""
20-
Start a kaleido server which will process all sync generation requests.
21-
22-
Only one server can be started at a time.
23-
24-
This wrapper function takes the exact same arguments as kaleido.Kaleido().
25-
"""
26-
_global_server.open(*args, **kwargs)
27-
28-
29-
def stop_sync_server():
30-
"""Stop the kaleido server. It can be restarted."""
31-
_global_server.close()
32-
33-
3415
__all__ = [
3516
"Kaleido",
3617
"PageGenerator",
@@ -46,6 +27,40 @@ def stop_sync_server():
4627
"write_fig_sync",
4728
]
4829

30+
_global_server = _sync_server.GlobalKaleidoServer()
31+
32+
33+
def start_sync_server(*args, silence_warnings=False, **kwargs):
34+
"""
35+
Start a kaleido server which will process all sync generation requests.
36+
37+
The kaleido server is a singleton, so it can't be opened twice. This
38+
function will warn you if the server is already running.
39+
40+
This wrapper function takes the exact same arguments as kaleido.Kaleido(),
41+
except one extra, `silence_warnings`.
42+
43+
Args:
44+
*args: all arguments `Kaleido()` would take.
45+
silence_warnings: (bool, default False): If True, don't emit warning if
46+
starting an already started server.
47+
**kwargs: all keyword arguments `Kaleido()` would take.
48+
49+
"""
50+
_global_server.open(*args, silence_warnings=silence_warnings, **kwargs)
51+
52+
53+
def stop_sync_server(*, silence_warnings=False):
54+
"""
55+
Stop the kaleido server. It can be restarted. Warns if not started.
56+
57+
Args:
58+
silence_warnings: (bool, default False): If True, don't emit warning if
59+
stopping a server that's not running.
60+
61+
"""
62+
_global_server.close(silence_warnings=silence_warnings)
63+
4964

5065
async def calc_fig(
5166
fig,

src/py/kaleido/_sync_server.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import atexit
45
import warnings
6+
from functools import partial
57
from queue import Queue
68
from threading import Thread
79
from typing import TYPE_CHECKING, NamedTuple
@@ -53,30 +55,38 @@ def __new__(cls):
5355
def is_running(self):
5456
return self._initialized
5557

56-
def open(self, *args: Any, **kwargs: Any) -> None:
58+
def open(self, *args: Any, silence_warnings=False, **kwargs: Any) -> None:
5759
"""Initialize the singleton with three values."""
5860
if self.is_running():
59-
warnings.warn(
60-
"Server already open.",
61-
RuntimeWarning,
62-
stacklevel=2,
63-
)
61+
if not silence_warnings:
62+
warnings.warn(
63+
"Server already open.",
64+
RuntimeWarning,
65+
stacklevel=2,
66+
)
6467
return
6568
coroutine = self._server(*args, **kwargs)
66-
self._thread: Thread = Thread(target=asyncio.run, args=(coroutine,))
69+
self._thread: Thread = Thread(
70+
target=asyncio.run,
71+
args=(coroutine,),
72+
daemon=True,
73+
)
6774
self._task_queue: Queue[Task | None] = Queue()
6875
self._return_queue: Queue[Any] = Queue()
6976
self._thread.start()
7077
self._initialized = True
78+
close = partial(self.close, silence_warnings=True)
79+
atexit.register(close)
7180

72-
def close(self):
81+
def close(self, *, silence_warnings=False):
7382
"""Reset the singleton back to an uninitialized state."""
7483
if not self.is_running():
75-
warnings.warn(
76-
"Server already closed.",
77-
RuntimeWarning,
78-
stacklevel=2,
79-
)
84+
if not silence_warnings:
85+
warnings.warn(
86+
"Server already closed.",
87+
RuntimeWarning,
88+
stacklevel=2,
89+
)
8090
return
8191
self._task_queue.put(None)
8292
self._thread.join()

0 commit comments

Comments
 (0)