|
@@ -3,6 +3,7 @@ r"""
|
|
.. autofunction:: run_asyncio_coroutine
|
|
.. autofunction:: run_asyncio_coroutine
|
|
.. autofunction:: register_thread
|
|
.. autofunction:: register_thread
|
|
.. autofunction:: defer_call
|
|
.. autofunction:: defer_call
|
|
|
|
+.. autofunction:: hold
|
|
|
|
|
|
.. autoclass:: pywebio.session.coroutinebased.TaskHandle
|
|
.. autoclass:: pywebio.session.coroutinebased.TaskHandle
|
|
:members:
|
|
:members:
|
|
@@ -15,12 +16,12 @@ from .base import AbstractSession
|
|
from .coroutinebased import CoroutineBasedSession
|
|
from .coroutinebased import CoroutineBasedSession
|
|
from .threadbased import ThreadBasedSession, ScriptModeSession
|
|
from .threadbased import ThreadBasedSession, ScriptModeSession
|
|
from ..exceptions import SessionNotFoundException
|
|
from ..exceptions import SessionNotFoundException
|
|
-from ..utils import iscoroutinefunction, isgeneratorfunction
|
|
|
|
|
|
+from ..utils import iscoroutinefunction, isgeneratorfunction, run_as_function, to_coroutine
|
|
|
|
|
|
# 当前进程中正在使用的会话实现的列表
|
|
# 当前进程中正在使用的会话实现的列表
|
|
_active_session_cls = []
|
|
_active_session_cls = []
|
|
|
|
|
|
-__all__ = ['run_async', 'run_asyncio_coroutine', 'register_thread']
|
|
|
|
|
|
+__all__ = ['run_async', 'run_asyncio_coroutine', 'register_thread', 'hold', 'defer_call']
|
|
|
|
|
|
|
|
|
|
def register_session_implement_for_target(target_func):
|
|
def register_session_implement_for_target(target_func):
|
|
@@ -72,6 +73,8 @@ def get_current_task_id():
|
|
|
|
|
|
def check_session_impl(session_type):
|
|
def check_session_impl(session_type):
|
|
def decorator(func):
|
|
def decorator(func):
|
|
|
|
+ """装饰器:在函数调用前检查当前会话实现是否满足要求"""
|
|
|
|
+
|
|
@wraps(func)
|
|
@wraps(func)
|
|
def inner(*args, **kwargs):
|
|
def inner(*args, **kwargs):
|
|
curr_impl = get_session_implement()
|
|
curr_impl = get_session_implement()
|
|
@@ -92,6 +95,35 @@ def check_session_impl(session_type):
|
|
return decorator
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
+def chose_impl(gen_func):
|
|
|
|
+ """根据当前会话实现来将 gen_func 转化为协程对象或直接以函数运行"""
|
|
|
|
+
|
|
|
|
+ @wraps(gen_func)
|
|
|
|
+ def inner(*args, **kwargs):
|
|
|
|
+ gen = gen_func(*args, **kwargs)
|
|
|
|
+ if get_session_implement() == CoroutineBasedSession:
|
|
|
|
+ return to_coroutine(gen)
|
|
|
|
+ else:
|
|
|
|
+ return run_as_function(gen)
|
|
|
|
+
|
|
|
|
+ return inner
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@chose_impl
|
|
|
|
+def next_client_event():
|
|
|
|
+ res = yield get_current_session().next_client_event()
|
|
|
|
+ return res
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@chose_impl
|
|
|
|
+def hold():
|
|
|
|
+ """保持会话,直到用户关闭浏览器,
|
|
|
|
+ 此时函数抛出 `SessionClosedException <pywebio.exceptions.SessionClosedException>` 异常
|
|
|
|
+ """
|
|
|
|
+ while True:
|
|
|
|
+ yield next_client_event()
|
|
|
|
+
|
|
|
|
+
|
|
@check_session_impl(CoroutineBasedSession)
|
|
@check_session_impl(CoroutineBasedSession)
|
|
def run_async(coro_obj):
|
|
def run_async(coro_obj):
|
|
"""异步运行协程对象。协程中依然可以调用 PyWebIO 交互函数。 仅能在基于协程的会话上下文中调用
|
|
"""异步运行协程对象。协程中依然可以调用 PyWebIO 交互函数。 仅能在基于协程的会话上下文中调用
|