[Python] Python / Discord: "Unclosed Connector connections"

Discussão em 'Python' iniciado por Stack, Maio 18, 2025.

  1. Stack

    Stack Membro Participativo

    The below script connects to discord, and then disconnects again. that works perfectly and prints out these two items:


    **** Logged in!


    **** End
    ​

    But when the script ends, I get this additional warning:


    Unclosed connector


    connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7f18f84e6080>, 3702249.404694462)]']


    connector: <aiohttp.connector.TCPConnector object at 0x7f18f70af4c0>
    ​

    I tried many ways but have not figured out how to et around this; any pointers are welcome. Of course I could simply ignore it, but I'd prefer to handle this "graciously".

    Update: Sharing how I have approached it so far. The code I pasted is a minimum working example out of a larger code that allows reproducing the problem easily.

    I did try:

    • to define a session (aiohttp.ClientSession()) and then awaiting to close it
    • leverage the @client.event "on_disconnect()" to close the session
    • use "bot.run" rather than "client.run"; and even put that into a async run
    • add a sleep delay to see whether then the connection would be closed before the script ends
    • figure it out by looking at the discord examples (link)
    • study the similar stackoverflow posts and see whether I could get them to work for my case, such as here, here and here

    Thus any pointers that guide me on the right path are highly appreciated. If people still feel my question is "unprepared", appreciate a comment as to how I could make it better...

    import discord
    import asyncio

    # Suppress login messages from discord
    import logging
    logging.getLogger("discord.client").setLevel(logging.WARNING)
    logging.getLogger("discord.gateway").setLevel(logging.WARNING)

    intents = discord.Intents.default()
    client = discord.Client(intents=intents)

    @client.event
    async def on_ready():
    print('**** Logged in!')
    await client.close()

    # Starts the script:
    DISCORD_BOT_TOKEN = 'xxx'
    if __name__ == '__main__':
    client.run(DISCORD_BOT_TOKEN)
    print('**** End')

    Continue reading...

Compartilhe esta Página