r""" .. autofunction:: run_async .. autofunction:: run_asyncio_coroutine .. autofunction:: register_thread .. autoclass:: pywebio.session.coroutinebased.TaskHandle :members: """ import threading from functools import wraps from .base import AbstractSession from .coroutinebased import CoroutineBasedSession from .threadbased import ThreadBasedSession, ScriptModeSession from ..exceptions import SessionNotFoundException from ..utils import iscoroutinefunction, isgeneratorfunction # 当前进程中正在使用的会话实现的列表 _active_session_cls = [] __all__ = ['run_async', 'run_asyncio_coroutine', 'register_thread'] def register_session_implement_for_target(target_func): """根据target_func函数类型注册会话实现,并返回会话实现""" if iscoroutinefunction(target_func) or isgeneratorfunction(target_func): cls = CoroutineBasedSession else: cls = ThreadBasedSession if cls not in _active_session_cls: _active_session_cls.append(cls) return cls def get_session_implement(): """获取当前会话实现。仅供内部实现使用。应在会话上下文中调用""" if not _active_session_cls: _active_session_cls.append(ScriptModeSession) _start_script_mode_server() # 当前正在使用的会话实现只有一个 if len(_active_session_cls) == 1: return _active_session_cls[0] # 当前有多个正在使用的会话实现 for cls in _active_session_cls: try: cls.get_current_session() return cls except SessionNotFoundException: pass raise SessionNotFoundException def _start_script_mode_server(): from ..platform.tornado import start_server_in_current_thread_session start_server_in_current_thread_session() def get_current_session() -> "AbstractSession": return get_session_implement().get_current_session() def get_current_task_id(): return get_session_implement().get_current_task_id() def check_session_impl(session_type): def decorator(func): @wraps(func) def inner(*args, **kwargs): curr_impl = get_session_implement() # Check if 'now_impl' is a derived from session_type or is the same class if not issubclass(curr_impl, session_type): func_name = getattr(func, '__name__', str(func)) require = getattr(session_type, '__name__', str(session_type)) curr = getattr(curr_impl, '__name__', str(curr_impl)) raise RuntimeError("Only can invoke `{func_name:s}` in {require:s} context." " You are now in {curr:s} context".format(func_name=func_name, require=require, curr=curr)) return func(*args, **kwargs) return inner return decorator @check_session_impl(CoroutineBasedSession) def run_async(coro_obj): """异步运行协程对象。协程中依然可以调用 PyWebIO 交互函数。 仅能在基于协程的会话上下文中调用 :param coro_obj: 协程对象 :return: An instance of `TaskHandle ` is returned, which can be used later to close the task. """ return get_current_session().run_async(coro_obj) @check_session_impl(CoroutineBasedSession) async def run_asyncio_coroutine(coro_obj): """若会话线程和运行事件的线程不是同一个线程,需要用 run_asyncio_coroutine 来运行asyncio中的协程。 仅能在基于协程的会话上下文中调用 :param coro_obj: 协程对象 """ return await get_current_session().run_asyncio_coroutine(coro_obj) @check_session_impl(ThreadBasedSession) def register_thread(thread: threading.Thread): """注册线程,以便在线程内调用 PyWebIO 交互函数。仅能在基于线程的会话上下文中调用 :param threading.Thread thread: 线程对象 """ return get_current_session().register_thread(thread)