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

332
Views
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 answers
Answer question

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 Report
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!