5
5
import asyncio
6
6
import os
7
7
from http .server import ThreadingHTTPServer
8
- from typing import Any , Callable , Coroutine , Optional , TypeVar , overload
8
+ from typing import Any , Awaitable , Callable , Coroutine , Optional , TypeVar , overload
9
9
from urllib .parse import urlsplit
10
10
11
11
from typing_extensions import ParamSpec , TypeAlias
@@ -96,7 +96,7 @@ async def main(coro: Coroutine[Any, Any, T], addr: Optional[str] = None) -> T:
96
96
97
97
98
98
def run (coro : Coroutine [Any , Any , T ], addr : Optional [str ] = None ) -> T :
99
- """Run the default dispatch server. The default server uses a function
99
+ """Run the default Dispatch server. The default server uses a function
100
100
registry where functions tagged by the `@dispatch.function` decorator are
101
101
registered.
102
102
@@ -119,9 +119,27 @@ def run(coro: Coroutine[Any, Any, T], addr: Optional[str] = None) -> T:
119
119
return asyncio .run (main (coro , addr ))
120
120
121
121
122
- def run_forever ():
123
- """Run the default dispatch server forever."""
124
- return run (asyncio .Event ().wait ())
122
+ def run_forever (
123
+ coro : Optional [Coroutine [Any , Any , T ]] = None , addr : Optional [str ] = None
124
+ ):
125
+ """Run the default Dispatch server forever.
126
+
127
+ Args:
128
+ coro: A coroutine to optionally run as the entrypoint.
129
+
130
+ addr: The address to bind the server to. If not provided, the server
131
+ will bind to the address specified by the `DISPATCH_ENDPOINT_ADDR`
132
+ environment variable. If the environment variable is not set, the
133
+ server will bind to `localhost:8000`.
134
+ """
135
+ wait = asyncio .Event ().wait ()
136
+ coro = chain (coro , wait ) if coro is not None else wait
137
+ return run (coro = coro , addr = addr )
138
+
139
+
140
+ async def chain (* awaitables : Awaitable [Any ]):
141
+ for a in awaitables :
142
+ await a
125
143
126
144
127
145
def batch () -> Batch :
0 commit comments