skip to Main Content

I’m trying to connect with my Google Cloud Sql db using google.cloud.sql.connector, and this is the code to connect:

def getconn():
    with Connector() as connector:
        conn = connector.connect(
            instance_connection_name,
            "pg8000",
            user = db_user,
            password = db_pass,
            db = db_name,
            ip_type = IPTypes.PUBLIC
        )
        return conn


# create connection pool
pool = sqlalchemy.create_engine(
    "postgresql+pg8000://",
    creator=getconn
)

and this is the code to do a simple select:

with pool.connect() as db_conn:
    # insert into database
    #db_conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})

    # query database
    result = db_conn.execute(sqlalchemy.text("SELECT * from users")).fetchall()

    # Do something with the results
    for row in result:
        print(row)
connector.close()

now when i run the code, the connection are ok and i print all records of table into db, but after i receive this error:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000026D24CD0A60>
Traceback (most recent call last):
  File "C:Program FilesPython310libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:Program FilesPython310libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:Program FilesPython310libasynciobase_events.py", line 750, in call_soon
    self._check_closed()
  File "C:Program FilesPython310libasynciobase_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000026D24CD0A60>
Traceback (most recent call last):
  File "C:Program FilesPython310libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:Program FilesPython310libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:Program FilesPython310libasynciobase_events.py", line 750, in call_soon
    self._check_closed()
  File "C:Program FilesPython310libasynciobase_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

How can I fix it?

2

Answers


  1. Chosen as BEST ANSWER

    the issue is on Windows SO, so I have fixed using module platform and asyncio, with this snippet code:

    import platform
    if platform.system()=='Windows':
       asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    

  2. This issue is caused by the fact that your calling connector.close() on a context manager object.

    When using the connector as a context manager you do not need to explicitly close the connector object, the context manager does this for you. You only need to call connector.close() when you explicitly initialize the connector object WITHOUT a context manager like so:

    from google.cloud.sql.connector import Connector
    
    connector = Connector()
    # your database connection/pooling code
    # ...
    connector.close
    

    So for your specific case just remove your connector.close() call and you will no longer see the error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search