My project have the following structure:
I want to add eureka to my fastapi applicaction, I´'m trying to do that in the init.py file:
import py_eureka_client.eureka_client as eureka_client
from fastapi import FastAPI
app = FastAPI()
eureka_client.init(eureka_server="http://eureka-primary:8011/eureka/,http://eureka-secondary:8012/eureka/,http://eureka-tertiary:8013/eureka/",
app_name="your_app_name",
instance_port=8000)
Also I'm using uvicorn to run the app like this:
uvicorn landus.main:app --reload
But I get the error:
RuntimeError: Cannot run the event loop while another loop is running
I saw a few links like this https://github.com/keijack/python-eureka-client/issues/76 but I don't understand how it works, I only know that fastapi or uvicorn are async application and when I add eureka, that is another async application is where a get this error, but don't know how to make it work
I make it work executing the init_async function and using asynccontextmanager form fast api:
import py_eureka_client.eureka_client as eureka_client
from fastapi import FastAPI
from contextlib import asynccontextmanager
from landus.controladores import controladorRouter
@asynccontextmanager
async def lifespan(app: FastAPI):
await eureka_client.init_async(
eureka_server="http://eureka-primary:8011/eureka/,http://eureka-secondary:8012/eureka/,http://eureka-tertiary:8013/eureka/",
app_name="msprueba",
instance_port=8000,
instance_host="localhost"
)
yield
app = FastAPI(lifespan=lifespan)
app.include_router(controladorRouter)
The problem was that I cannot execute a code that is also async with fastapi, so the lifespan function execute before fastapi run