Skip to content
Advertisement

Discord.py with threads, RuntimeError: Timeout context manager should be used inside a task

So I’m creating a discord bot that is constantly scraping a webpage, checking if the item is in stock. When the item becomes in stock, send a message in the chat. Since the scraping is in a constant loop, I figured I would put that in a thread, one for each page that needs to be scraped. The problem is that each thread is producing the same error,

JavaScript

Does anyone know why, or a better approach to what I am trying to do?

JavaScript

Advertisement

Answer

Some frameworks don’t like to run in threads – i.e. all GUI frameworks has to run windows and widgets in main thread. They are called not thread-safe.

And you may have similar problem – inside thread you can’t use async created in main thread.

You should run searchPage in new thread as normal function and send results to main thread which should send some message to discord. You can use global variable or better queue to send results. And main thread should run some function which periodically check queue and send messages to discord.

Discord has @tasks.loop(second=...) which can run periodically function.


Minimal working code

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement