Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

333
Vistas
Call async function in Enum

I want to refactor my code from sync to async. I use Python and FastAPI. I use the method which calls async function in Enumaration.

For example:

from enum import Enum
from app.story import get_story

    StoriesEnum = Enum(
        "StoriesEnum", {story: story  for story in get_story.story_list},
    )

get_story is an async function that returns Story class and it has story_list.

How can I await the get_story.story_list?

I tried:

  • asyncio.run()
  • get_event_loop()
  • async generator

with no successful result. They don't work because await is outside the async function.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

As per the documentation:

You might have noticed that await can only be used inside of functions defined with async def.

But at the same time, functions defined with async def have to be "awaited". So, functions with async def can only be called inside of functions defined with async def too.

Thus, what you could do is:

import asyncio

async def go(): 
    return Enum("StoriesEnum", {s:s for s in (await get_story()).story_list.value})

StoriesEnum = asyncio.run(go())
print({e:e.value for e in StoriesEnum})

P.S. It would really help though, if you provided a minimal reproducible example.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda