Giter Site home page Giter Site logo

Comments (4)

davidvoler avatar davidvoler commented on June 14, 2024 2
from fastapi import FastAPI
from uvicorn import Config, Server

import yappi
import sys
import asyncio
import random
from logging import getLogger

logger = getLogger(__name__)
current_module = sys.modules[__name__]
yappi.set_clock_type("WALL") 

app = FastAPI()




async def handle_messages():
    r = random.random()
    # r = 3
    await asyncio.sleep(r)
    logger.error(f"waited for {r} seconds")
    


async def listener():
    logger.error("listener started")
    while True:
        asyncio.create_task(handle_messages())
        await asyncio.sleep(1)


@app.on_event("startup")
async def startup():
    asyncio.create_task(listener())

@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/stats")
async def stats():
    stats = yappi.get_func_stats(
        filter_callback=lambda x: yappi.module_matches(x, [current_module])
    )
    stats.sort("totaltime", "desc").print_all()
    stats.strip_dirs()
    stats.print_all()
    yappi.clear_stats()
    ret = []
    for s in stats:
        ret.append((s[15],s[3], s[14], s[6]))
    return ret


if __name__ == "__main__":
    with yappi.run():
        config = Config(app)
        server = Server(config)
        server.run()

Save the the above code as main.py
The follwing code is working when running :
python main.py

But it will not work if you run:
uvicorn main:app
This make sense as when you run with uvicorn you skip the if name == "main": part

I am happy with this workaround.
Will it work for you @MorAlterya ?

from yappi.

kwuite avatar kwuite commented on June 14, 2024

@MorAlterya

Have you tried returning results first and then executing yappi.stop()?

yappi.start()  # 'profile_threads=True' default value

uvicorn.run("main:app")

yappi.get_func_stats().print_all()
yappi.get_threads_stats().print_all()
yappi.stop()

from yappi.

sumerc avatar sumerc commented on June 14, 2024

@kwuite , it should not make any difference if the profiler is stopped or not.

@MorAlterya , could you please provide an example application that reproduces the problem. In theory, the code you have provided should just work fine, and we have many unit tests confirming it should work. I need to understand what is different in your case. What version of FastAPI, how you run it..etc. A simple example demonstrating this error would be very useful.

from yappi.

MorAlterya avatar MorAlterya commented on June 14, 2024

The exact configuration I provided produces results for the main thread only, tested it but calling endpoints and not even seeing the output.

FastAPI version: 0.88.0
yappi version: 1.4.0

Our project no longer runs on FastAPI, we moved to a task-based system, and now Yappi works great!
We have excellent profiling from threads. Now none of them are created by uvicorn/FastAPI.

from yappi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.