coroutinebased.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. 基于协程的任务会话
  24. 当主协程任务和会话内所有通过 `run_async` 注册的协程都退出后,会话关闭。
  25. 当用户浏览器主动关闭会话,CoroutineBasedSession.close 被调用, 协程任务和会话内所有通过 `run_async` 注册的协程都被关闭。
  26. """
  27. _active_session_cnt = 0
  28. @classmethod
  29. def active_session_count(cls):
  30. return cls._active_session_cnt
  31. @staticmethod
  32. def get_current_session() -> "CoroutineBasedSession":
  33. if _context.current_session is None:
  34. raise SessionNotFoundException("No current found in context!")
  35. return _context.current_session
  36. @staticmethod
  37. def get_current_task_id():
  38. if _context.current_task_id is None:
  39. raise RuntimeError("No current task found in context!")
  40. return _context.current_task_id
  41. def __init__(self, target, on_task_command=None, on_session_close=None):
  42. """
  43. :param target: 协程函数
  44. :param on_task_command: 由协程内发给session的消息的处理函数
  45. :param on_session_close: 会话结束的处理函数。后端Backend在相应on_session_close时关闭连接时,需要保证会话内的所有消息都传送到了客户端
  46. """
  47. assert asyncio.iscoroutinefunction(target) or inspect.isgeneratorfunction(target), ValueError(
  48. "CoroutineBasedSession accept coroutine function or generator function as task function")
  49. CoroutineBasedSession._active_session_cnt += 1
  50. self._on_task_command = on_task_command or (lambda _: None)
  51. self._on_session_close = on_session_close or (lambda: None)
  52. self.unhandled_task_msgs = []
  53. self.coros = {} # coro_task_id -> coro
  54. self._closed = False
  55. self._not_closed_coro_cnt = 1 # 当前会话未结束运行的协程数量。当 self._not_closed_coro_cnt == 0 时,会话结束。
  56. main_task = Task(target(), session=self, on_coro_stop=self._on_task_finish)
  57. self.coros[main_task.coro_id] = main_task
  58. self._step_task(main_task)
  59. def _step_task(self, task, result=None):
  60. task.step(result)
  61. def _on_task_finish(self, task: "Task"):
  62. self._not_closed_coro_cnt -= 1
  63. if task.coro_id in self.coros:
  64. logger.debug('del self.coros[%s]', task.coro_id)
  65. del self.coros[task.coro_id]
  66. if self._not_closed_coro_cnt <= 0:
  67. self.send_task_command(dict(command='close_session'))
  68. self._on_session_close()
  69. self.close()
  70. def send_task_command(self, command):
  71. """向会话发送来自协程内的消息
  72. :param dict command: 消息
  73. """
  74. self.unhandled_task_msgs.append(command)
  75. self._on_task_command(self)
  76. async def next_client_event(self):
  77. res = await WebIOFuture()
  78. return res
  79. def send_client_event(self, event):
  80. """向会话发送来自用户浏览器的事件️
  81. :param dict event: 事件️消息
  82. """
  83. coro_id = event['task_id']
  84. coro = self.coros.get(coro_id)
  85. if not coro:
  86. logger.error('coro not found, coro_id:%s', coro_id)
  87. return
  88. self._step_task(coro, event)
  89. def get_task_commands(self):
  90. msgs = self.unhandled_task_msgs
  91. self.unhandled_task_msgs = []
  92. return msgs
  93. def _cleanup(self):
  94. for t in self.coros.values():
  95. t.close()
  96. self.coros = {} # delete session tasks
  97. CoroutineBasedSession._active_session_cnt -= 1
  98. def close(self):
  99. """关闭当前Session。由Backend调用"""
  100. if self._closed:
  101. return
  102. self._closed = True
  103. self._cleanup()
  104. # todo clean
  105. def closed(self):
  106. return self._closed
  107. def on_task_exception(self):
  108. from ..output import put_markdown # todo
  109. logger.exception('Error in coroutine executing')
  110. type, value, tb = sys.exc_info()
  111. tb_len = len(list(traceback.walk_tb(tb)))
  112. lines = traceback.format_exception(type, value, tb, limit=1 - tb_len)
  113. traceback_msg = ''.join(lines)
  114. put_markdown("发生错误:\n```\n%s\n```" % traceback_msg)
  115. def register_callback(self, callback, mutex_mode=False):
  116. """ 向Session注册一个回调函数,返回回调id
  117. :type callback: Callable or Coroutine
  118. :param callback: 回调函数. 可以是普通函数或者协程函数. 函数签名为 ``callback(data)``.
  119. :param bool mutex_mode: 互斥模式。若为 ``True`` ,则在运行回调函数过程中,无法响应同一组件的新点击事件,仅当 ``callback`` 为协程函数时有效
  120. :return str: 回调id.
  121. CoroutineBasedSession 保证当收到前端发送的事件消息 ``{event: "callback",coro_id: 回调id, data:...}`` 时,
  122. ``callback`` 回调函数被执行, 并传入事件消息中的 ``data`` 字段值作为参数
  123. """
  124. async def callback_coro():
  125. while True:
  126. event = await self.next_client_event()
  127. assert event['event'] == 'callback'
  128. coro = None
  129. if asyncio.iscoroutinefunction(callback):
  130. coro = callback(event['data'])
  131. elif inspect.isgeneratorfunction(callback):
  132. coro = asyncio.coroutine(callback)(event['data'])
  133. else:
  134. try:
  135. callback(event['data'])
  136. except:
  137. CoroutineBasedSession.get_current_session().on_task_exception()
  138. if coro is not None:
  139. if mutex_mode:
  140. await coro
  141. else:
  142. self.run_async(coro)
  143. callback_task = Task(callback_coro(), CoroutineBasedSession.get_current_session())
  144. callback_task.coro.send(None) # 激活,Non't callback.step() ,导致嵌套调用step todo 与inactive_coro_instances整合
  145. CoroutineBasedSession.get_current_session().coros[callback_task.coro_id] = callback_task
  146. return callback_task.coro_id
  147. def run_async(self, coro_obj):
  148. """异步运行协程对象。可以在协程内调用 PyWebIO 交互函数
  149. :param coro_obj: 协程对象
  150. :return: An instance of `TaskHandle` is returned, which can be used later to close the task.
  151. """
  152. self._not_closed_coro_cnt += 1
  153. task = Task(coro_obj, session=self, on_coro_stop=self._on_task_finish)
  154. self.coros[task.coro_id] = task
  155. asyncio.get_event_loop().call_soon(task.step)
  156. return task.task_handle()
  157. async def run_asyncio_coroutine(self, coro_obj):
  158. """若会话线程和运行事件的线程不是同一个线程,需要用 asyncio_coroutine 来运行asyncio中的协程"""
  159. res = await WebIOFuture(coro=coro_obj)
  160. return res
  161. class TaskHandle:
  162. def __init__(self, close, closed):
  163. self._close = close
  164. self._closed = closed
  165. def close(self):
  166. """关闭任务"""
  167. return self._close()
  168. def closed(self):
  169. """返回任务是否关闭"""
  170. return self._closed()
  171. class Task:
  172. @contextmanager
  173. def session_context(self):
  174. """
  175. >>> with session_context():
  176. ... res = self.coros[-1].send(data)
  177. """
  178. # todo issue: with 语句可能发生嵌套,导致内层with退出时,将属性置空
  179. _context.current_session = self.session
  180. _context.current_task_id = self.coro_id
  181. try:
  182. yield
  183. finally:
  184. _context.current_session = None
  185. _context.current_task_id = None
  186. @staticmethod
  187. def gen_coro_id(coro=None):
  188. name = 'coro'
  189. if hasattr(coro, '__name__'):
  190. name = coro.__name__
  191. return '%s-%s' % (name, random_str(10))
  192. def __init__(self, coro, session: CoroutineBasedSession, on_coro_stop=None):
  193. self.session = session
  194. self.coro = coro
  195. self.coro_id = None
  196. self.result = None
  197. self.task_closed = False # 任务完毕/取消
  198. self.on_coro_stop = on_coro_stop or (lambda: None)
  199. self.coro_id = self.gen_coro_id(self.coro)
  200. self.pending_futures = {} # id(future) -> future
  201. logger.debug('Task[%s] created ', self.coro_id)
  202. def step(self, result=None):
  203. coro_yield = None
  204. with self.session_context():
  205. try:
  206. coro_yield = self.coro.send(result)
  207. except StopIteration as e:
  208. if len(e.args) == 1:
  209. self.result = e.args[0]
  210. self.task_closed = True
  211. logger.debug('Task[%s] finished', self.coro_id)
  212. self.on_coro_stop(self)
  213. except Exception as e:
  214. self.session.on_task_exception()
  215. self.task_closed = True
  216. self.on_coro_stop(self)
  217. future = None
  218. if isinstance(coro_yield, WebIOFuture):
  219. if coro_yield.coro:
  220. future = asyncio.run_coroutine_threadsafe(coro_yield.coro, asyncio.get_event_loop())
  221. elif coro_yield is not None:
  222. future = coro_yield
  223. if not self.session.closed() and hasattr(future, 'add_done_callback'):
  224. future.add_done_callback(self._tornado_future_callback)
  225. self.pending_futures[id(future)] = future
  226. def _tornado_future_callback(self, future):
  227. if not future.cancelled():
  228. del self.pending_futures[id(future)]
  229. self.step(future.result())
  230. def close(self):
  231. if self.task_closed:
  232. return
  233. logger.debug('Task[%s] closed', self.coro_id)
  234. self.coro.close()
  235. while self.pending_futures:
  236. _, f = self.pending_futures.popitem()
  237. f.cancel()
  238. self.task_closed = True
  239. self.on_coro_stop(self)
  240. def __del__(self):
  241. if not self.task_closed:
  242. logger.warning('Task[%s] not finished when destroy', self.coro_id)
  243. def task_handle(self):
  244. handle = TaskHandle(close=self.close, closed=lambda: self.task_closed)
  245. return handle