From baefd09f657a7ea1d6f1f6ea1c0dd28a387378ce Mon Sep 17 00:00:00 2001 From: blank X Date: Wed, 16 Sep 2020 21:52:26 +0700 Subject: [PATCH] Fix timeout --- redditbot.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/redditbot.py b/redditbot.py index ea59a1f..7ffd7d7 100644 --- a/redditbot.py +++ b/redditbot.py @@ -95,13 +95,16 @@ async def main(): show_spoilers_warning = chat_data['show_spoilers_warning'] timeout = chat_data.get('timeout') - def _get_submission_blocc(): - while True: + give_ups = set() + def _get_submission_blocc(unique_id): + while unique_id not in give_ups: subreddit = reddit.subreddit(random.choice(subreddits)) random_post = subreddit.random() cpid = cpp = None if random_post is None: for submission in subreddit.hot(): + if unique_id in give_ups: + return cpid = getattr(submission, 'crosspost_parent', None) if cpid and getattr(random_post, 'crosspost_parent_list', None): cpid = cpid[3:] @@ -158,14 +161,17 @@ async def main(): @aiocron.crontab(cron_duration) async def start_post(): while True: + unique_id = time.time() try: - random_post, cpp = await asyncio.wait_for(client.loop.run_in_executor(None, _get_submission_blocc), timeout) + random_post, cpp = await asyncio.wait_for(client.loop.run_in_executor(None, _get_submission_blocc, unique_id), timeout) except asyncio.TimeoutError: + give_ups.add(unique_id) logging.error('%s timed out', chat) for i in bot_admins: await client.send_message(i, f'{chat} timed out') break except Exception: + give_ups.add(unique_id) logging.exception(chat) for i in bot_admins: await client.send_message(i, f'{chat}\n{traceback.format_exc()}')