Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
¿Cómo usar el grupo de conexiones AsyncElasticsearch en FastApi?

Las partes relevantes de mi código son las siguientes:

 @app.on_event("startup") async def startup(): es = Translator() app.state.es_conn = es.connect() @app.on_event('shutdown') async def shutdown_event(): es = Translator() await es.close(app.state.es_conn) @app.post('/query') async def query(req: Request): es = Translator() es.set_conn(req.app.state.es_conn) return await es.search() from elasticsearch import AsyncElasticsearch, AsyncTransport class Translator(object): def __init__(self): self.__es = None def connect(self): return AsyncElasticsearch( hosts='http://192.168.0.2:9200/', transport_class=AsyncTransport) def set_conn(self, conn): self.__es = conn async def close(self, conn): await conn.close() self.__es = None async def search(self): return await self.__es.search(index=index, body=body)

Se ejecuta en el siguiente entorno:

  • PyPy 7.3.7
  • fastapi 0.73.0
  • Búsqueda elástica 7.10.1

mi pregunta son:

  1. Aunque el código puede funcionar normalmente, ¿cómo puedo detectar si usa un grupo de conexiones?
  2. ¿Cómo usar el grupo de conexiones correctamente cuando es asíncrono?

¿Alguien puede ayudarme? ¡Gracias!

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!