Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Psycopg: Unable to connect in async mode with asyncio on Windows #1623

Open
atul-vasudevan opened this issue Dec 17, 2024 · 0 comments
Assignees

Comments

@atul-vasudevan
Copy link

atul-vasudevan commented Dec 17, 2024

Expected Behavior

The application should connect to the database without errors when running in async mode.

Actual Behavior

The application throws the error mentioned above and fails to connect.

Steps to Reproduce

  • Create a Python Azure Functions app structured with Blueprints.
  • Add asyncio logic and attempt to establish an async connection with Psycopg.
  • Run the app on Windows.

Relevant code being tried

``

import asyncio
import azure.functions as func
import datetime
import json
import logging

from psycopg_pool import AsyncConnectionPool

app=func.FunctionApp()
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

@app.route(route="test")
async def main(req: func.HttpRequest) -> func.HttpResponse:    
  logging.info('Python HTTP trigger function processed a request.')   
  # Database connection parameters    
  conn_info = "dbname=test user=testpassword=123 host=localhost port=5432"    
  try:        
  pool = AsyncConnectionPool(conninfo=conn_info )        # Connect to the database       
  async with pool.connection() as conn:            
      async with conn.cursor() as cur:                # Execute the query                
          await cur.execute("SELECT * FROM test LIMIT 1;")                
          row = await cur.fetchone()                # Process the result                
          if row:                    
              return func.HttpResponse(f"Row: {row}", status_code=200)                
          else:                    
              return func.HttpResponse("No rows found", status_code=404)    
  except Exception as e:        
      logging.error(f"Error: {e}")        
  return func.HttpResponse(f"Error: {e}", status_code=500)

Relevant log output

error connecting in 'pool-1': Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'

requirements.txt file

azure-functions==1.18.0
psycopg-binary==3.2.3
psycopg-pool==3.2.4
psycopg==3.2.3

Where are you facing this problem?

Local - Core Tools

Function app name

No response

Additional Information

I am facing an issue with my Python Azure Functions application (v2), which is structured using Blueprints. When I add asyncio to the application and try to run it on Windows, it throws the error mentioned above.
I have tried the solution mentioned in various pages where the event loop policy is explicitly set as shown below:

import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

I have tried adding this in function_app.py but the issue persists. I am using python 3.11.

I would appreciate any guidance on resolving this issue or insights into potential misconfigurations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants