Browse Source

Make sure that websockets.connect is a coroutine.

In 3.5.0 and 3.5.1 asyncio.ensure_future requires a Future or a
coroutine otherwise a TypeError is raised. The issue is that the
websockets.connect call is an awaitable rather than a coroutine.

asyncio.ensure_future did not gain support for awaitables until 3.5.2.
This patch allows 3.5.0 and 3.5.1 to connect regardless of their python
version.
pull/572/head
Rapptz 8 years ago
parent
commit
e557f69c83
  1. 13
      discord/shard.py

13
discord/shard.py

@ -68,6 +68,16 @@ class Shard:
return self._current
@asyncio.coroutine
def _ensure_coroutine_connect(gateway, loop):
# In 3.5+ websockets.connect does not return a coroutine, but an awaitable.
# The problem is that in 3.5.0 and in some cases 3.5.1, asyncio.ensure_future and
# by proxy, asyncio.wait_for, do not accept awaitables, but rather futures or coroutines.
# By wrapping it up into this function we ensure that it's in a coroutine and not an awaitable
# even for 3.5.0 users.
ws = yield from websockets.connect(gateway, loop=loop, klass=DiscordWebSocket)
return ws
class AutoShardedClient(Client):
"""A client similar to :class:`Client` except it handles the complications
of sharding for the user into a more manageable and transparent single
@ -182,8 +192,7 @@ class AutoShardedClient(Client):
@asyncio.coroutine
def launch_shard(self, gateway, shard_id):
try:
ws = yield from asyncio.wait_for(websockets.connect(gateway, loop=self.loop, klass=DiscordWebSocket),
loop=self.loop, timeout=180.0)
ws = yield from asyncio.wait_for(_ensure_coroutine_connect(gateway, self.loop), loop=self.loop, timeout=180.0)
except Exception as e:
log.info('Failed to connect for shard_id: %s. Retrying...', shard_id)
yield from asyncio.sleep(5.0, loop=self.loop)

Loading…
Cancel
Save