Refactor loop code to allow usage of asyncio.run

This commit is contained in:
Han Seung Min - 한승민
2022-03-13 14:24:14 +05:30
committed by GitHub
parent 196db33e9f
commit 93af158b0c
9 changed files with 44 additions and 136 deletions

View File

@ -177,12 +177,11 @@ class View:
self.timeout = timeout
self.children: List[Item] = self._init_children()
self.__weights = _ViewWeights(self.children)
loop = asyncio.get_running_loop()
self.id: str = os.urandom(16).hex()
self.__cancel_callback: Optional[Callable[[View], None]] = None
self.__timeout_expiry: Optional[float] = None
self.__timeout_task: Optional[asyncio.Task[None]] = None
self.__stopped: asyncio.Future[bool] = loop.create_future()
self.__stopped: asyncio.Future[bool] = asyncio.get_running_loop().create_future()
def __repr__(self) -> str:
return f'<{self.__class__.__name__} timeout={self.timeout} children={len(self.children)}>'
@ -379,12 +378,11 @@ class View:
def _start_listening_from_store(self, store: ViewStore) -> None:
self.__cancel_callback = partial(store.remove_view)
if self.timeout:
loop = asyncio.get_running_loop()
if self.__timeout_task is not None:
self.__timeout_task.cancel()
self.__timeout_expiry = time.monotonic() + self.timeout
self.__timeout_task = loop.create_task(self.__timeout_task_impl())
self.__timeout_task = asyncio.create_task(self.__timeout_task_impl())
def _dispatch_timeout(self):
if self.__stopped.done():