Skip to content

Commit 3662f6c

Browse files
add support for async server function
1 parent 3d1108c commit 3662f6c

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

shiny/_app.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from contextlib import AsyncExitStack, asynccontextmanager
77
from inspect import signature
88
from pathlib import Path
9-
from typing import Any, Callable, Mapping, Optional, TypeVar, cast
9+
from typing import Any, Awaitable, Callable, Mapping, Optional, TypeVar, cast
1010

1111
import starlette.applications
1212
import starlette.exceptions
@@ -57,8 +57,8 @@ class App:
5757
returns a UI definition, if you need the UI definition to be created dynamically
5858
for each pageview.
5959
server
60-
A function which is called once for each session, ensuring that each session is
61-
independent.
60+
A sync or async function which is called once for each session, ensuring that
61+
each session is independent.
6262
static_assets
6363
Static files to be served by the app. If this is a string or Path object, it
6464
must be a directory, and it will be mounted at `/`. If this is a dictionary,
@@ -104,13 +104,13 @@ def server(input: Inputs, output: Outputs, session: Session):
104104
"""
105105

106106
ui: RenderedHTML | Callable[[Request], Tag | TagList]
107-
server: Callable[[Inputs, Outputs, Session], None]
107+
server: Callable[[Inputs, Outputs, Session], Awaitable[None] | None]
108108

109109
def __init__(
110110
self,
111111
ui: Tag | TagList | Callable[[Request], Tag | TagList] | Path,
112112
server: (
113-
Callable[[Inputs], None] | Callable[[Inputs, Outputs, Session], None] | None
113+
Callable[[Inputs], Awaitable[None] | None] | Callable[[Inputs, Outputs, Session], Awaitable[None] | None] | None
114114
),
115115
*,
116116
static_assets: Optional[str | Path | Mapping[str, str | Path]] = None,

shiny/session/_session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,9 @@ def verify_state(expected_state: ConnectionState) -> None:
633633
self._manage_inputs(message_obj["data"])
634634

635635
with session_context(self):
636-
self.app.server(self.input, self.output, self)
636+
result = self.app.server(self.input, self.output, self)
637+
if isinstance(result, Awaitable):
638+
await result
637639

638640
elif message_obj["method"] == "update":
639641
verify_state(ConnectionState.Running)

0 commit comments

Comments
 (0)