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

@ -95,7 +95,6 @@ class Shard:
self._client: Client = client
self._dispatch: Callable[..., None] = client.dispatch
self._queue_put: Callable[[EventItem], None] = queue_put
self.loop: asyncio.AbstractEventLoop = self._client.loop
self._disconnect: bool = False
self._reconnect = client._reconnect
self._backoff: ExponentialBackoff = ExponentialBackoff()
@ -115,7 +114,7 @@ class Shard:
return self.ws.shard_id # type: ignore
def launch(self) -> None:
self._task = self.loop.create_task(self.worker())
self._task = self._client.loop.create_task(self.worker())
def _cancel_task(self) -> None:
if self._task is not None and not self._task.done():
@ -318,10 +317,10 @@ class AutoShardedClient(Client):
if TYPE_CHECKING:
_connection: AutoShardedConnectionState
def __init__(self, *args: Any, loop: Optional[asyncio.AbstractEventLoop] = None, **kwargs: Any) -> None:
def __init__(self, *args: Any, **kwargs: Any) -> None:
kwargs.pop('shard_id', None)
self.shard_ids: Optional[List[int]] = kwargs.pop('shard_ids', None)
super().__init__(*args, loop=loop, **kwargs)
super().__init__(*args, **kwargs)
if self.shard_ids is not None:
if self.shard_count is None:
@ -348,7 +347,6 @@ class AutoShardedClient(Client):
handlers=self._handlers,
hooks=self._hooks,
http=self.http,
loop=self.loop,
**options,
)