Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

334
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda