Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

python - Spammed with errors from asyncpg when discord bot is turned off

I am trying to integrate asyncpg with discord.py, however I ran into one very anoyying issue.

Whenever I try to stop the bot using ^C, I get spammed with a ton of errors. This is just bothersome as when I'm trying to debug something I often lose the original error.

Here is my code:

loop = asyncio.get_event_loop()

async def connection_init(conn):
    await conn.execute("SET CLIENT_ENCODING to 'utf-8';")
    conn.client = client

try:
    client.pool = loop.run_until_complete(asyncpg.create_pool(
        host=os.environ.get("postgres_host"),
        database=os.environ.get("postgres_database"),
        user=os.environ.get("postgres_user"),
        password=os.environ.get("postgres_password"),
        connection_class=dbutils.DBUtils,
        init=connection_init
    ))

    print('PostgreSQL connection successful')
except Exception as e:
    print(e)

    # the bot basically cannot function without database
    print('PostgreSQL connection failed- aborting')
    exit()


client.run(os.environ.get("main"))

Here are the errors that floods my terminal. It's the same error however it pops up like 20 times.

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x033DF148>
Traceback (most recent call last):
  File "C:UserszghanAppDataLocalProgramsPythonPython38-32libasyncioproactor_events.py", line 116, in __del__
  File "C:UserszghanAppDataLocalProgramsPythonPython38-32libasyncioproactor_events.py", line 108, in close
  File "C:UserszghanAppDataLocalProgramsPythonPython38-32libasyncioase_events.py", line 719, in call_soon
  File "C:UserszghanAppDataLocalProgramsPythonPython38-32libasyncioase_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
question from:https://stackoverflow.com/questions/65838092/spammed-with-errors-from-asyncpg-when-discord-bot-is-turned-off

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That error happens because the asyncio.get_event_loop() can not retrieve any loop, as at the moment you try to retrieve it, it is already closed.

The correct syntax would be to start a new loop instead of trying to get the current:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(asyncio.new_event_loop())
# ...
loop = asyncio.get_event_loop()

Alternatively, in Python 3.7 asyncio added a new way of managing the loops: using asyncio.run(), which does not require you to create, set or retrieve the current loop as it already manages all that internally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...