Hi, maybe that's a stupid idea but I wanted to try and run discord bot in separate thread. I can start the bot but of course closing it gives an error of "got Future <Future pending> attached to a different loop", I understand that's because discord is running in another thread.
I know daemon thread will exit anyway and eventually connection to discord will timeout but I was wondering if there's anyway to issue stop command to the bot in thread, any tips?
run_loop = True
bot = discord.Bot()
def my_bot():
bot.run(cfg["discord_token"])
bot_thread = threading.Thread(target=my_bot, daemon=True)
async def loop():
bot_thread.start()
while run_loop:
try:
logging.info("This is a test")
except Exception as exc:
logging.error(f"Main error: {exc}")
await asyncio.sleep(2)
await bot.close()
logging.info("Loop exited")
def signal_handler(signum, frame):
logging.info("Stopping the program...")
global run_loop
run_loop = False
signal.signal(signal.SIGINT, signal_handler)
if __name__ == "__main__":
asyncio.run(loop())