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`)
| 721 | |
| 722 | |
| 723 | class Runner(object): |
| 724 | """Internal implementation of `tornado.gen.coroutine`. |
| 725 | |
| 726 | Maintains information about pending callbacks and their results. |
| 727 | |
| 728 | The results of the generator are stored in ``result_future`` (a |
| 729 | `.Future`) |
| 730 | """ |
| 731 | |
| 732 | def __init__( |
| 733 | self, |
| 734 | ctx_run: Callable, |
| 735 | gen: "Generator[_Yieldable, Any, _T]", |
| 736 | result_future: "Future[_T]", |
| 737 | first_yielded: _Yieldable, |
| 738 | ) -> None: |
| 739 | self.ctx_run = ctx_run |
| 740 | self.gen = gen |
| 741 | self.result_future = result_future |
| 742 | self.future = _null_future # type: Union[None, Future] |
| 743 | self.running = False |
| 744 | self.finished = False |
| 745 | self.io_loop = IOLoop.current() |
| 746 | if self.handle_yield(first_yielded): |
| 747 | gen = result_future = first_yielded = None # type: ignore |
| 748 | self.ctx_run(self.run) |
| 749 | |
| 750 | def run(self) -> None: |
| 751 | """Starts or resumes the generator, running until it reaches a |
| 752 | yield point that is not ready. |
| 753 | """ |
| 754 | if self.running or self.finished: |
| 755 | return |
| 756 | try: |
| 757 | self.running = True |
| 758 | while True: |
| 759 | future = self.future |
| 760 | if future is None: |
| 761 | raise Exception("No pending future") |
| 762 | if not future.done(): |
| 763 | return |
| 764 | self.future = None |
| 765 | try: |
| 766 | exc_info = None |
| 767 | |
| 768 | try: |
| 769 | value = future.result() |
| 770 | except Exception: |
| 771 | exc_info = sys.exc_info() |
| 772 | future = None |
| 773 | |
| 774 | if exc_info is not None: |
| 775 | try: |
| 776 | yielded = self.gen.throw(*exc_info) # type: ignore |
| 777 | finally: |
| 778 | # Break up a reference to itself |
| 779 | # for faster GC on CPython. |
| 780 | exc_info = None |