1717class Session :
1818 """Session for the engine."""
1919
20- def __init__ (self , session_id : int , session_mgr : ' SessionManager' , ** kwargs ):
20+ def __init__ (self , session_id : int , session_mgr : SessionManager , ** kwargs ):
2121 self .session_id = session_id
2222 self .prompt : Any = None
2323 self .response : Response | None = None
@@ -26,8 +26,8 @@ def __init__(self, session_id: int, session_mgr: 'SessionManager', **kwargs):
2626 self .step : int = 0
2727 # event to wait for the session to be active
2828 self ._active : asyncio .Event | None = None
29- self ._hnd = None # inference instance
30- self ._session_mgr : ' SessionManager' = weakref .ref (session_mgr )
29+ self ._handle = None # inference instance
30+ self ._session_mgr : SessionManager = weakref .ref (session_mgr )
3131 self .update (** kwargs )
3232
3333 def update (self , ** kwargs ):
@@ -65,22 +65,22 @@ def reset(self):
6565 self .gen_config = None
6666 self .step = 0
6767 self ._active = None
68- self ._hnd = None
69- del self ._session_mgr
68+ self ._handle = None
69+ self ._session_mgr = None
7070 logger .debug (f'Session { self .session_id } has been reset.' )
7171
7272 @asynccontextmanager
7373 async def request_handle (self ):
74- if self ._hnd is not None :
74+ if self ._handle is not None :
7575 raise RuntimeError (f'Session { self .session_id } already has an inference instance.' )
7676 logger .debug (f'[acquire_request_handle] session { self .session_id } acquiring an instance' )
7777
78- hnd_pool = self ._session_mgr ().request_handle_pool
79- self ._hnd = await hnd_pool .get ()
78+ hnd_pool = self ._session_mgr ().request_handle_pool # 要不要有 ()
79+ self ._handle = await hnd_pool .get ()
8080 self ._active = asyncio .Event ()
8181 logger .debug (f'[acquire_request_handle] session { self .session_id } acquired an instance' )
8282 try :
83- yield self ._hnd
83+ yield self ._handle
8484 except SafeRunException :
8585 # SafeRunException is raised by AsyncEngine.safe_run. We don't need to handle it here.
8686 pass
@@ -90,28 +90,26 @@ async def request_handle(self):
9090 finally :
9191 logger .debug (f'[acquire_request_handle] session { self .session_id } releasing the instance' )
9292 # Return inference instance if it was acquired
93- if self ._hnd is not None :
94- hnd_pool .put (self ._hnd )
95- self ._hnd = None
93+ if self ._handle is not None :
94+ hnd_pool .put (self ._handle )
95+ self ._handle = None
9696 # MUST set the signal after releasing the instance to avoid race condition
9797 # refer to async_end method
9898 self ._active .set ()
9999
100100 async def async_abort (self ):
101101 """Abort the session."""
102102 logger .debug (f'Aborting session { self .session_id } ' )
103- if self ._hnd is not None :
104- await self ._hnd .async_cancel (self .session_id )
103+ if self ._handle is not None :
104+ await self ._handle .async_cancel (self .session_id )
105105 # DO NOT reset the session here because it might be used by other components.
106106 # Leave the cleanup to the caller.
107107
108108 async def async_close (self ):
109109 """End the session."""
110110 logger .debug (f'Ending session { self .session_id } ' )
111- if self ._hnd is not None :
111+ if self ._handle is not None :
112112 await self ._active .wait ()
113- if self ._hnd is not None :
114- raise RuntimeError (f'Session { self .session_id } is not finished yet.' )
115113 async with self .request_handle () as handle :
116114 try :
117115 await handle .async_end (self .session_id )
0 commit comments