Selaa lähdekoodia

refine: detect and start Script mode

wangweimin 5 vuotta sitten
vanhempi
säilyke
5905082034
1 muutettua tiedostoa jossa 6 lisäystä ja 22 poistoa
  1. 6 22
      pywebio/session/__init__.py

+ 6 - 22
pywebio/session/__init__.py

@@ -16,8 +16,7 @@ from .coroutinebased import CoroutineBasedSession
 from .threadbased import ThreadBasedSession, ScriptModeSession
 from .threadbased import ThreadBasedSession, ScriptModeSession
 from ..exceptions import SessionNotFoundException
 from ..exceptions import SessionNotFoundException
 
 
-
-_session_type = ThreadBasedSession
+_session_type = None
 
 
 __all__ = ['run_async', 'run_asyncio_coroutine', 'register_thread']
 __all__ = ['run_async', 'run_asyncio_coroutine', 'register_thread']
 
 
@@ -33,38 +32,23 @@ def set_session_implement_for_target(target_func):
 
 
 def get_session_implement():
 def get_session_implement():
     global _session_type
     global _session_type
+    if _session_type is None:
+        _session_type = ScriptModeSession
+        _start_script_mode_server()
     return _session_type
     return _session_type
 
 
 
 
 def _start_script_mode_server():
 def _start_script_mode_server():
-    global _session_type
     from ..platform.tornado import start_server_in_current_thread_session
     from ..platform.tornado import start_server_in_current_thread_session
-    _session_type = ScriptModeSession
     start_server_in_current_thread_session()
     start_server_in_current_thread_session()
 
 
 
 
 def get_current_session() -> "AbstractSession":
 def get_current_session() -> "AbstractSession":
-    try:
-        return _session_type.get_current_session()
-    except SessionNotFoundException:
-        # 如果没已经运行的backend server,在当前线程上下文作为session启动backend server
-        if get_session_implement().active_session_count() == 0:
-            _start_script_mode_server()
-            return _session_type.get_current_session()
-        else:
-            raise
+    return get_session_implement().get_current_session()
 
 
 
 
 def get_current_task_id():
 def get_current_task_id():
-    try:
-        return _session_type.get_current_task_id()
-    except RuntimeError:
-        # 如果没已经运行的backend server,在当前线程上下文作为session启动backend server
-        if get_session_implement().active_session_count() == 0:
-            _start_script_mode_server()
-            return _session_type.get_current_session()
-        else:
-            raise
+    return get_session_implement().get_current_task_id()
 
 
 
 
 def check_session_impl(session_type):
 def check_session_impl(session_type):