@@ -253,14 +253,19 @@ def on_startup():
253253 import nest_asyncio
254254 nest_asyncio .apply ()
255255 logger .info (f"Starting server on port { port } (Colab mode)" )
256+
257+ # Define the callback for Colab
258+ async def on_startup_async ():
259+ on_startup ()
260+
256261 config = uvicorn .Config (
257262 app ,
258263 host = "0.0.0.0" ,
259264 port = port ,
260265 reload = False ,
261266 log_level = "info" ,
262- # Add callback to print the RUNNING banner when server starts
263- callback_notify = [ on_startup ]
267+ # Use an async callback function, not a list
268+ callback_notify = on_startup_async
264269 )
265270 server = uvicorn .Server (config )
266271 asyncio .get_event_loop ().run_until_complete (server .serve ())
@@ -278,8 +283,13 @@ async def serve(self, sockets=None):
278283 self .config .setup_event_loop ()
279284 await self .startup (sockets = sockets )
280285 # Call our callback before processing requests
281- for callback in self .config .callback_notify :
282- callback ()
286+ if callable (self .config .callback_notify ):
287+ await self .config .callback_notify ()
288+ elif isinstance (self .config .callback_notify , list ):
289+ for callback in self .config .callback_notify :
290+ if callable (callback ):
291+ callback ()
292+ on_startup () # Always ensure our startup function is called
283293 await self .main_loop ()
284294 await self .shutdown ()
285295
@@ -290,7 +300,8 @@ async def serve(self, sockets=None):
290300 reload = False ,
291301 workers = 1 ,
292302 log_level = "info" ,
293- callback_notify = [on_startup ]
303+ # This won't be used directly, as we call on_startup in the ServerWithCallback class
304+ callback_notify = None
294305 )
295306 server = ServerWithCallback (config )
296307 asyncio .run (server .serve ())
0 commit comments