coroutinebased.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import asyncio
  2. import inspect
  3. import logging
  4. import sys
  5. import traceback
  6. from contextlib import contextmanager
  7. from .base import AbstractSession
  8. from ..exceptions import SessionNotFoundException
  9. from ..utils import random_str
  10. logger = logging.getLogger(__name__)
  11. class WebIOFuture:
  12. def __init__(self, coro=None):
  13. self.coro = coro
  14. def __iter__(self):
  15. result = yield self
  16. return result
  17. __await__ = __iter__ # make compatible with 'await' expression
  18. class _context:
  19. current_session = None # type:"AsyncBasedSession"
  20. current_task_id = None
  21. class CoroutineBasedSession(AbstractSession):
  22. """
  23. 一个PyWebIO任务会话, 由不同的后端Backend创建并维护
  24. WebIOSession是不同的后端Backend与协程交互的桥梁:
  25. 后端Backend在接收到用户浏览器的数据后,会通过调用 ``send_client_event`` 来通知会话,进而由Session驱动协程的运行。
  26. Task内在调用输入输出函数后,会调用 ``send_task_command`` 向会话发送输入输出消息指令, Session将其保存并留给后端Backend处理。
  27. .. note::
  28. 后端Backend在相应on_session_close时关闭连接时,需要保证会话内的所有消息都传送到了客户端
  29. """
  30. @staticmethod
  31. def get_current_session() -> "CoroutineBasedSession":
  32. if _context.current_session is None:
  33. raise SessionNotFoundException("No current found in context!")
  34. return _context.current_session
  35. @staticmethod
  36. def get_current_task_id():
  37. if _context.current_task_id is None:
  38. raise RuntimeError("No current task found in context!")
  39. return _context.current_task_id
  40. def __init__(self, coroutine_func, on_task_command=None, on_session_close=None):
  41. """
  42. :param coroutine_func: 协程函数
  43. :param on_task_command: 由协程内发给session的消息的处理函数
  44. :param on_session_close: 会话结束的处理函数。后端Backend在相应on_session_close时关闭连接时,需要保证会话内的所有消息都传送到了客户端
  45. """
  46. self._on_task_command = on_task_command or (lambda _: None)
  47. self._on_session_close = on_session_close or (lambda: None)
  48. self.unhandled_task_msgs = []
  49. self.coros = {} # coro_task_id -> coro
  50. self._closed = False
  51. self.inactive_coro_instances = [] # 待激活的协程实例列表
  52. self.main_task = Task(coroutine_func(), session=self, on_coro_stop=self._on_main_task_finish)
  53. self.coros[self.main_task.coro_id] = self.main_task
  54. self._step_task(self.main_task)
  55. def _step_task(self, task, result=None):
  56. task.step(result)
  57. if task.task_finished and task.coro_id in self.coros:
  58. # 若task 为main task,则 task.step(result) 结束后,可能task已经结束,self.coros已被清理
  59. logger.debug('del self.coros[%s]', task.coro_id)
  60. del self.coros[task.coro_id]
  61. while self.inactive_coro_instances and not self.main_task.task_finished:
  62. coro = self.inactive_coro_instances.pop()
  63. sub_task = Task(coro, session=self)
  64. self.coros[sub_task.coro_id] = sub_task
  65. sub_task.step()
  66. if sub_task.task_finished:
  67. logger.debug('del self.coros[%s]', sub_task.coro_id)
  68. del self.coros[sub_task.coro_id]
  69. def _on_main_task_finish(self):
  70. self.send_task_command(dict(command='close_session'))
  71. self.close()
  72. def send_task_command(self, command):
  73. """向会话发送来自协程内的消息
  74. :param dict command: 消息
  75. """
  76. self.unhandled_task_msgs.append(command)
  77. self._on_task_command(self)
  78. async def next_client_event(self):
  79. res = await WebIOFuture()
  80. return res
  81. def send_client_event(self, event):
  82. """向会话发送来自用户浏览器的事件️
  83. :param dict event: 事件️消息
  84. """
  85. coro_id = event['task_id']
  86. coro = self.coros.get(coro_id)
  87. if not coro:
  88. logger.error('coro not found, coro_id:%s', coro_id)
  89. return
  90. self._step_task(coro, event)
  91. def get_task_commands(self):
  92. msgs = self.unhandled_task_msgs
  93. self.unhandled_task_msgs = []
  94. return msgs
  95. def _cleanup(self):
  96. for t in self.coros.values():
  97. t.close()
  98. self.coros = {} # delete session tasks
  99. while self.inactive_coro_instances:
  100. coro = self.inactive_coro_instances.pop()
  101. coro.close()
  102. def close(self, no_session_close_callback=False):
  103. """关闭当前Session
  104. :param bool no_session_close_callback: 不调用 on_session_close 会话结束的处理函数。
  105. 当 close 是由后端Backend调用时可能希望开启 no_session_close_callback
  106. """
  107. self._cleanup()
  108. self._closed = True
  109. if not no_session_close_callback:
  110. self._on_session_close()
  111. # todo clean
  112. def closed(self):
  113. return self._closed
  114. def on_task_exception(self):
  115. from ..output import put_markdown # todo
  116. logger.exception('Error in coroutine executing')
  117. type, value, tb = sys.exc_info()
  118. tb_len = len(list(traceback.walk_tb(tb)))
  119. lines = traceback.format_exception(type, value, tb, limit=1 - tb_len)
  120. traceback_msg = ''.join(lines)
  121. put_markdown("发生错误:\n```\n%s\n```" % traceback_msg)
  122. def register_callback(self, callback, mutex_mode=False):
  123. """ 向Session注册一个回调函数,返回回调id
  124. :type callback: Callable or Coroutine
  125. :param callback: 回调函数. 可以是普通函数或者协程函数. 函数签名为 ``callback(data)``.
  126. :param bool mutex_mode: 互斥模式。若为 ``True`` ,则在运行回调函数过程中,无法响应同一组件的新点击事件,仅当 ``callback`` 为协程函数时有效
  127. :return str: 回调id.
  128. CoroutineBasedSession 保证当收到前端发送的事件消息 ``{event: "callback",coro_id: 回调id, data:...}`` 时,
  129. ``callback`` 回调函数被执行, 并传入事件消息中的 ``data`` 字段值作为参数
  130. """
  131. async def callback_coro():
  132. while True:
  133. event = await self.next_client_event()
  134. assert event['event'] == 'callback'
  135. coro = None
  136. if asyncio.iscoroutinefunction(callback):
  137. coro = callback(event['data'])
  138. elif inspect.isgeneratorfunction(callback):
  139. coro = asyncio.coroutine(callback)(event['data'])
  140. else:
  141. try:
  142. callback(event['data'])
  143. except:
  144. CoroutineBasedSession.get_current_session().on_task_exception()
  145. if coro is not None:
  146. if mutex_mode:
  147. await coro
  148. else:
  149. self.run_async(coro)
  150. callback_task = Task(callback_coro(), CoroutineBasedSession.get_current_session())
  151. callback_task.coro.send(None) # 激活,Non't callback.step() ,导致嵌套调用step todo 与inactive_coro_instances整合
  152. CoroutineBasedSession.get_current_session().coros[callback_task.coro_id] = callback_task
  153. return callback_task.coro_id
  154. def run_async(self, coro_obj):
  155. self.inactive_coro_instances.append(coro_obj)
  156. async def run_asyncio_coroutine(self, coro_obj):
  157. """若会话线程和运行事件的线程不是同一个线程,需要用 asyncio_coroutine 来运行asyncio中的协程"""
  158. res = await WebIOFuture(coro=coro_obj)
  159. return res
  160. class Task:
  161. @contextmanager
  162. def session_context(self):
  163. """
  164. >>> with session_context():
  165. ... res = self.coros[-1].send(data)
  166. """
  167. # todo issue: with 语句可能发生嵌套,导致内层with退出时,将属性置空
  168. _context.current_session = self.session
  169. _context.current_task_id = self.coro_id
  170. try:
  171. yield
  172. finally:
  173. _context.current_session = None
  174. _context.current_task_id = None
  175. @staticmethod
  176. def gen_coro_id(coro=None):
  177. name = 'coro'
  178. if hasattr(coro, '__name__'):
  179. name = coro.__name__
  180. return '%s-%s' % (name, random_str(10))
  181. def __init__(self, coro, session: CoroutineBasedSession, on_coro_stop=None):
  182. self.session = session
  183. self.coro = coro
  184. self.coro_id = None
  185. self.result = None
  186. self.task_finished = False # 任务完毕/取消
  187. self.on_coro_stop = on_coro_stop or (lambda: None)
  188. self.coro_id = self.gen_coro_id(self.coro)
  189. self.pending_futures = {} # id(future) -> future
  190. logger.debug('Task[%s] created ', self.coro_id)
  191. def step(self, result=None):
  192. coro_yield = None
  193. with self.session_context():
  194. try:
  195. coro_yield = self.coro.send(result)
  196. except StopIteration as e:
  197. if len(e.args) == 1:
  198. self.result = e.args[0]
  199. self.task_finished = True
  200. logger.debug('Task[%s] finished', self.coro_id)
  201. self.on_coro_stop()
  202. except Exception as e:
  203. self.session.on_task_exception()
  204. future = None
  205. if isinstance(coro_yield, WebIOFuture):
  206. if coro_yield.coro:
  207. future = asyncio.run_coroutine_threadsafe(coro_yield.coro, asyncio.get_event_loop())
  208. elif coro_yield is not None:
  209. future = coro_yield
  210. if not self.session.closed() and hasattr(future, 'add_done_callback'):
  211. future.add_done_callback(self._tornado_future_callback)
  212. self.pending_futures[id(future)] = future
  213. def _tornado_future_callback(self, future):
  214. if not future.cancelled():
  215. del self.pending_futures[id(future)]
  216. self.step(future.result())
  217. def close(self):
  218. logger.debug('Task[%s] closed', self.coro_id)
  219. self.coro.close()
  220. while self.pending_futures:
  221. _, f = self.pending_futures.popitem()
  222. f.cancel()
  223. self.task_finished = True
  224. def __del__(self):
  225. if not self.task_finished:
  226. logger.warning('Task[%s] not finished when destroy', self.coro_id)