Hey everyone tonite I released the latest version of MicroPie, my ultra micro ASGI framework inspired by CherryPy’s method based routing.
This release focused on improving the frameworks ability to handle large file uploads. We introduced the new _parse_multipart_into_request for live request population, added bounded asyncio.Queues for file parts to enforce backpressure, and updated _asgi_app_http to start parsing in background.
With these improvements we can now handle large file uploads, successfully tested with a 20GB video file.
You can check out and star the project on Github: patx/micropie. We are still in active development. MicroPie provides an easy way to learn more about ASGI and modern Python web devlopment. If you have any questions or issues please file a issue report on the Github page.
The file uploads are performant. They benchmark better then FastAPI which uses Startlet to handle multipart uploads. Other frameworks like Quart and Sanic are not able to handle uploads of this size at this time (unless I’m doing something wrong!). Check out the benchmarks for a 14.4 GB .mkv file. The first is MicroPie, the second is FastAPI: $ time curl -X POST -F « file=@video.mkv » http://127.0.0.1:8000/upload Uploaded video.mkv real 0m41.273s user 0m0.675s sys 0m12.197s $ time curl -X POST -F « file=@video.mkv » http://127.0.0.1:8000/upload {« status »: »ok », »filename »: »video.mkv »} real 1m50.060s user 0m0.908s sys 0m15.743s
The code used for FastAPI: « ` import os import aiofiles from fastapi import FastAPI, UploadFile, File from fastapi.responses import HTMLResponse
os.makedirs(« uploads », exist_ok=True) app = FastAPI()
@app.get(« / », response_class=HTMLResponse) async def index(): return « » »<form action= »/upload » method= »post » enctype= »multipart/form-data »> <input type= »file » name= »file » required> <input type= »submit » value= »Upload »> </form> » » »
@app.post(« /upload ») async def upload(file: UploadFile = File(…)): filepath = os.path.join(« uploads », file.filename) async with aiofiles.open(filepath, « wb ») as f: while chunk := await file.read(): await f.write(chunk) return {« status »: « ok », « filename »: file.filename} « `
And the code used for MicroPie: « ` import os import aiofiles from micropie import App
os.makedirs(« uploads », exist_ok=True)
class Root(App): async def index(self): return « » » <form action= »/upload » method= »post » enctype= »multipart/form-data »> <input type= »file » name= »file » required> <input type= »submit » value= »Upload »> </form> » » »
async def upload(self, file): filepath = os.path.join(« uploads », file[« filename »]) async with aiofiles.open(filepath, « wb ») as f: while chunk := await file[« content »].get(): await f.write(chunk) return f »Uploaded {file[‘filename’]} »
app = Root() « `
submitted by /u/Miserable_Ear3789 to r/Python
[link] [comments]
Laisser un commentaire