Skip to content

Commit

Permalink
Save file in chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Aug 8, 2023
1 parent 9978c34 commit d8ce887
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions agent/python/agent_protocol/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import os

import aiofiles
from fastapi import APIRouter, UploadFile
from fastapi.responses import FileResponse
from hypercorn.asyncio import serve
Expand Down Expand Up @@ -146,8 +147,9 @@ async def upload_agent_task_artifacts(
if not os.path.exists(path):
os.makedirs(path)

with open(os.path.join(path, file.filename), "wb") as f:
f.write(await file.read())
async with aiofiles.open(os.path.join(path, file.filename), "wb") as f:
while content := await file.read(1024 * 1024): # async read chunk ~1MiB
await f.write(content)

return artifact

Expand Down
13 changes: 12 additions & 1 deletion agent/python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pydantic = "^1.10.5, <2"
click = "^8.1.6"
requests = "^2.31.0"
python-multipart = "^0.0.6"
aiofiles = "^23.1.0"

[tool.poetry.group.dev.dependencies]
fastapi-code-generator = "^0.4.2"
Expand Down

0 comments on commit d8ce887

Please sign in to comment.