From ce205daa0eac74b67412eda6d700650e090de4c8 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Tue, 5 Mar 2019 19:38:40 +0000 Subject: [PATCH] Try a new create task and lower max_messages by half --- discordbot/titanembeds/bot.py | 9 +++++++-- discordbot/titanembeds/redisqueue.py | 12 +++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/discordbot/titanembeds/bot.py b/discordbot/titanembeds/bot.py index c81b50b..acdff1a 100644 --- a/discordbot/titanembeds/bot.py +++ b/discordbot/titanembeds/bot.py @@ -21,9 +21,14 @@ logging.getLogger('sqlalchemy') # except raven.exceptions.InvalidDsn: # pass +try: + create_task = asyncio.ensure_future +except AttributeError: + create_task = getattr(asyncio, 'async') + class Titan(discord.AutoShardedClient): def __init__(self): - super().__init__(max_messages=20000) + super().__init__(max_messages=10000) self.aiosession = aiohttp.ClientSession(loop=self.loop) self.http.user_agent += ' TitanEmbeds-Bot' self.redisqueue = RedisQueue(self, config["redis-uri"]) @@ -63,7 +68,7 @@ class Titan(discord.AutoShardedClient): async def start(self): await self.redisqueue.connect() - self.loop.create_task(self.redisqueue.subscribe()) + create_task(self.redisqueue.subscribe()) await super().start(config["bot-token"]) async def on_ready(self): diff --git a/discordbot/titanembeds/redisqueue.py b/discordbot/titanembeds/redisqueue.py index 14bcb4d..39e6181 100644 --- a/discordbot/titanembeds/redisqueue.py +++ b/discordbot/titanembeds/redisqueue.py @@ -8,6 +8,11 @@ import traceback import sys import re +try: + create_task = asyncio.ensure_future +except AttributeError: + create_task = getattr(asyncio, 'async') + class RedisQueue: def __init__(self, bot, redis_uri): self.bot = bot @@ -37,10 +42,7 @@ class RedisQueue: subscriber = await self.sub_connection.start_subscribe() await subscriber.subscribe(["discord-api-req"]) count = 0 - while True: - if not self.bot.is_ready() or self.bot.is_closed(): - await asyncio.sleep(0) - continue + while not self.bot.is_closed(): reply = await subscriber.next_published() request = json.loads(reply.value) resource = request["resource"] @@ -54,7 +56,7 @@ class RedisQueue: def dispatch(self, event, key, params): method = "on_" + event if hasattr(self, method): - self.bot.loop.create_task(self._run_event(method, key, params)) + create_task(self._run_event(method, key, params)) async def _run_event(self, event, key, params): try: