Comments (4)
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.
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.
@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.
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)
- Missing tag for 1.3.5 release HOT 3
- Tests are broken on Python 3.11 due to @asyncio.coroutine use HOT 1
- yappi fails to build with python 3.11
- suggestion: add a column showing max execution time for a single call HOT 6
- `test_gevent` seems to depend on `3.10`
- Support more architectures in Github Actions
- Openssl related Error install Yappi with pip on pyton 3.11 OS:Windows HOT 1
- Python run `gevent` tests on CI for `3.11`
- New 3.11 compatible release? HOT 3
- base class names incorrect HOT 2
- Question: does yappi add "considerable runtime overhead"? Can it be used in production? HOT 2
- sum(<genexpr>) not represented correctly in call graph
- Can't install on macOS HOT 2
- Uniqueness of the YFuncStat object
- Full path not printed HOT 2
- could you make the git tag name pattern consistent? HOT 1
- Matplotlib errors with TypeError: 'NoneType' object is not callable HOT 4
- Fastapi support HOT 3
- Not able to install yappi in ubuntu 18.04 using pip HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yappi.