Internal implementation of `tornado.gen.coroutine`. Maintains information about pending callbacks and their results. The results of the generator are stored in ``result_future`` (a `.Future`)
| 737 | |
| 738 | |
| 739 | class Runner: |
| 740 | """Internal implementation of `tornado.gen.coroutine`. |
| 741 | |
| 742 | Maintains information about pending callbacks and their results. |
| 743 | |
| 744 | The results of the generator are stored in ``result_future`` (a |
| 745 | `.Future`) |
| 746 | """ |
| 747 | |
| 748 | def __init__( |
| 749 | self, |
| 750 | ctx_run: Callable, |
| 751 | gen: "Generator[_Yieldable, Any, _T]", |
| 752 | result_future: "Future[_T]", |
| 753 | first_yielded: _Yieldable, |
| 754 | ) -> None: |
| 755 | self.ctx_run = ctx_run |
| 756 | self.gen = gen |
| 757 | self.result_future = result_future |
| 758 | self.future = _null_future # type: Union[None, Future] |
| 759 | self.running = False |
| 760 | self.finished = False |
| 761 | self.io_loop = IOLoop.current() |
| 762 | if self.ctx_run(self.handle_yield, first_yielded): |
| 763 | gen = result_future = first_yielded = None # type: ignore |
| 764 | self.ctx_run(self.run) |
| 765 | |
| 766 | def run(self) -> None: |
| 767 | """Starts or resumes the generator, running until it reaches a |
| 768 | yield point that is not ready. |
| 769 | """ |
| 770 | if self.running or self.finished: |
| 771 | return |
| 772 | try: |
| 773 | self.running = True |
| 774 | while True: |
| 775 | future = self.future |
| 776 | if future is None: |
| 777 | raise Exception("No pending future") |
| 778 | if not future.done(): |
| 779 | return |
| 780 | self.future = None |
| 781 | try: |
| 782 | try: |
| 783 | value = future.result() |
| 784 | except Exception as e: |
| 785 | # Save the exception for later. It's important that |
| 786 | # gen.throw() not be called inside this try/except block |
| 787 | # because that makes sys.exc_info behave unexpectedly. |
| 788 | exc: Optional[Exception] = e |
| 789 | else: |
| 790 | exc = None |
| 791 | finally: |
| 792 | future = None |
| 793 | |
| 794 | if exc is not None: |
| 795 | try: |
| 796 | yielded = self.gen.throw(exc) |