激活协程 :param any result: 向协程传入的数据 :param bool throw_exp: 是否向协程引发异常,为 True 时, result 参数为相应的异常对象
(self, result=None, throw_exp=False)
| 311 | logger.debug('Task[%s] created ', self.coro_id) |
| 312 | |
| 313 | def step(self, result=None, throw_exp=False): |
| 314 | """激活协程 |
| 315 | |
| 316 | :param any result: 向协程传入的数据 |
| 317 | :param bool throw_exp: 是否向协程引发异常,为 True 时, result 参数为相应的异常对象 |
| 318 | """ |
| 319 | coro_yield = None |
| 320 | with self.session_context(): |
| 321 | try: |
| 322 | if throw_exp: |
| 323 | coro_yield = self.coro.throw(result) |
| 324 | else: |
| 325 | coro_yield = self.coro.send(result) |
| 326 | except StopIteration as e: |
| 327 | if len(e.args) == 1: |
| 328 | self.result = e.args[0] |
| 329 | self.close() |
| 330 | logger.debug('Task[%s] finished', self.coro_id) |
| 331 | except Exception as e: |
| 332 | if not isinstance(e, SessionException): |
| 333 | self.session.on_task_exception() |
| 334 | self.close() |
| 335 | |
| 336 | if coro_yield is None: |
| 337 | return |
| 338 | |
| 339 | future = None |
| 340 | if isinstance(coro_yield, WebIOFuture): |
| 341 | if coro_yield.coro: |
| 342 | future = asyncio.run_coroutine_threadsafe(coro_yield.coro, asyncio.get_event_loop()) |
| 343 | else: |
| 344 | future = coro_yield |
| 345 | |
| 346 | if not self.session.closed() and hasattr(future, 'add_done_callback'): |
| 347 | future.add_done_callback(self._wakeup) |
| 348 | self.pending_futures[id(future)] = future |
| 349 | |
| 350 | def _wakeup(self, future): |
| 351 | if not future.cancelled(): |
no test coverage detected