question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Would expect this simple example to behave asynchronously but it doesn't

See original GitHub issue

Description

I’m probably not understanding the asynchronous concept correctly in FastAPI.

I’m accessing the root endpoint of the following app from two clients at the same time. I’d expect FastAPI to print Started twice:

from fastapi import FastAPI
import asyncio

app = FastAPI()

@app.get("/")
async def read_root():
    print('Started')
    await asyncio.sleep(5)
    print('Finished')
    return {"Hello": "World"}

Instead I get the following, which looks very much non asynchronous:

Started
Finished
INFO: ('127.0.0.1', 49655) - "GET / HTTP/1.1" 200
Started
Finished
INFO: ('127.0.0.1', 49655) - "GET / HTTP/1.1" 200

What am I missing?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
euri10commented, Oct 15, 2019

sorry misread you explain what you want and you’re right it should do that

I guess when you state that you used 2 clients at the same time, maybe that was not enough at the same time, no idea how you proceeded to launch concurrent requests

See this log launching 10 concurrent requests with

ab -n 10 -c 10 http://localhost:8000/

/home/lotsos/.local/share/virtualenvs/rapidfastapitest-s8GpZJ_e/bin/python /home/lotsos/PycharmProjects/rapidfastapitest/625_asyn_question.py
email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
INFO: Started server process [7007]
INFO: Waiting for application startup.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
2019-10-15 11:02:49,713 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,713 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47518) - "GET / HTTP/1.0" 200
2019-10-15 11:02:54,718 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,719 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,719 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,720 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,720 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,720 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,721 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,721 625_asyn_question DEBUG    Started
2019-10-15 11:02:54,721 625_asyn_question DEBUG    Started
2019-10-15 11:02:59,719 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47520) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,720 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47522) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,720 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47524) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,721 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47526) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,721 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47528) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,722 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47530) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,722 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47532) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,723 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47534) - "GET / HTTP/1.0" 200
2019-10-15 11:02:59,724 625_asyn_question DEBUG    Finished
INFO: ('127.0.0.1', 47536) - "GET / HTTP/1.0" 200

snippet used

import uvicorn
from fastapi import FastAPI
import asyncio

import logging

logger: logging.Logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

if not logger.hasHandlers():
    sh = logging.StreamHandler()
    fmt = logging.Formatter(fmt="%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
    sh.setFormatter(fmt)
    logger.addHandler(sh)
    logger.propagate = False



app = FastAPI()


@app.get("/")
async def read_root():
    logger.debug('Started')
    await asyncio.sleep(5)
    logger.debug('Finished')
    return {"Hello": "World"}



if __name__ == '__main__':
    uvicorn.run("625_asyn_question:app")
1reaction
tiangolocommented, Apr 10, 2020

Thanks for the help here @euri10 ! 👏 🙇

Thanks for reporting back and closing the issue @fhennecker 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async function not behaving as I expect with Jest
Async function not behaving as I expect with Jest · 1. If you don't pass done as an argument the code is considered...
Read more >
Error thrown - Warning: You called act(async () => ...) without ...
I have a simple test that seems to generate the right snapshot at the end of execution, but throws me a console.error during...
Read more >
When to Use (and Not to Use) Asynchronous Programming
Asynchronous programming is a form of parallel programming that allows a unit of work to run separately from the primary application thread.
Read more >
Async/await - The Modern JavaScript Tutorial
The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved...
Read more >
Introduction to Asynchronous JavaScript - Pluralsight
In this free JavaScript tutorial, we'll explain the difference between synchronous and asynchronous Javascript and the difficulties of using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found