coroutinebased.py 10 KB

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