您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

27 行
761 B

  1. from abc import ABCMeta, abstractmethod
  2. from types import TracebackType
  3. from typing import Optional, Type, TypeVar
  4. T = TypeVar("T")
  5. class AsyncResource(metaclass=ABCMeta):
  6. """
  7. Abstract base class for all closeable asynchronous resources.
  8. Works as an asynchronous context manager which returns the instance itself on enter, and calls
  9. :meth:`aclose` on exit.
  10. """
  11. async def __aenter__(self: T) -> T:
  12. return self
  13. async def __aexit__(self, exc_type: Optional[Type[BaseException]],
  14. exc_val: Optional[BaseException],
  15. exc_tb: Optional[TracebackType]) -> None:
  16. await self.aclose()
  17. @abstractmethod
  18. async def aclose(self) -> None:
  19. """Close the resource."""