Forums

Another "Event loop is closed" error

I currently have a functioning Discord bot that's an always-on task that's been running for a while now. I'm trying to make a second one, but I started getting the dreaded "Event loop is closed" error. I got a very simple "Hello world"-type client to run, and that caused the new bot to appear online in the discord guild. I then took code from my first bot and tried to run it as a second bot (using the second bot's token) but I started getting the "Event loop is closed" error, and now the "Hello world" code doesn't work either.

I saw another thread saying "you will have to run your discord bot with a uwsgi webserver instead of an eventloop webserver," but I don't know what any of those words mean. My old bot seems to work just fine. I have a paid account and it seems like I should have the ability to do what I'm trying to do. Any thoughts?

Could you run your script in a Bash console and show us the full error message (if there is one)?

.

Traceback (most recent call last):
  File "/home/Ffungo/mergebot.py", line 89, in <module>
    bot.run(TOKEN)
  File "/home/Ffungo/.local/lib/python3.8/site-packages/discord/client.py", line 695, in run
    loop.add_signal_handler(signal.SIGINT, lambda: loop.stop())
  File "/usr/lib/python3.8/asyncio/unix_events.py", line 89, in add_signal_handler
    self._check_closed()
  File "/usr/lib/python3.8/asyncio/base_events.py", line 504, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

[edit by admin: formatting]

Thanks for the details. That error is telling you that when the discord client library is trying to add on a handler to tell it what to do when you hit control-C, the event loop has already been closed. Are you doing anything in your code that might close the event loop? Or are you using multiple event loops for some reason, perhaps so that one running script can run multiple bots?

Hmm. I may have other event loops! I have the original bot running and I'm trying to get this bot running in parallel. The two bots are using different tokens. Is that not enough to prevent them from stepping on one another? (As you may have guessed, my career as a Discord bot maker is in the very early stages!)

Different tokens will probably not prevent them from interfiring with each other. You will need to run them in different proceses.

Run them in different processes. Ok. So I totally know how to do this, but maybe you could explain what that means (or point me to a resource) in case others don't?

I think that Glenn just meant that you can write each Discord bot as a separate script, just as if you were starting from scratch (with Python modules for any shared code, of course) and then run them as separate always-on tasks.