Created
November 1, 2023 03:56
-
-
Save cnmoro/ace46426063fc98e69a9152ec94abf3c to your computer and use it in GitHub Desktop.
fastapi_mongo_non_blocking_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uvicorn, asyncio | |
import motor.motor_asyncio | |
from fastapi import FastAPI | |
import pandas as pd | |
app = FastAPI() | |
client = motor.motor_asyncio.AsyncIOMotorClient() | |
db = client['MYDB'] | |
def process_data(data): | |
data = pd.DataFrame(data) | |
data = data.query("mycolumn == 'somevalue'") | |
return len(data) | |
@app.get("/") | |
async def test(): | |
documents = await db['mycollection'].find({}).to_list(length=None) # none = all documents | |
loop = asyncio.get_event_loop() | |
return await loop.run_in_executor(None, process_data, documents) | |
if __name__ == "__main__": | |
uvicorn.run(app, host="0.0.0.0", port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment