Skip to content

Commit 777e72c

Browse files
committed
🔧 fix large file upload by adjusting timeout
1 parent 0eab8d3 commit 777e72c

File tree

7 files changed

+37
-17
lines changed

7 files changed

+37
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
site
99
.python-version
1010
requirements-dev.lock
11+
.aider*

examples/big_upload.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from codeboxapi import CodeBox
2+
3+
with CodeBox() as codebox:
4+
with open("examples/assets/swedata/train/data-00000-of-00001.arrow", "rb") as file:
5+
codebox.upload(file.name, file.read(), timeout=900)
6+
7+
print(codebox.list_files())

examples/file_io.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
).content
99
codebox.upload("iris.csv", csv_bytes)
1010

11-
# install openpyxl for excel conversion
12-
codebox.install("pandas")
13-
codebox.install("openpyxl")
14-
1511
# convert dataset csv to excel
1612
output = codebox.run(
1713
"import pandas as pd\n\n"

src/codeboxapi/box/basebox.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Abstract Base Class for Isolated Execution Environments (CodeBox's) """
1+
"""Abstract Base Class for Isolated Execution Environments (CodeBox's)"""
22

33
from abc import ABC, abstractmethod
44
from datetime import datetime
@@ -46,11 +46,15 @@ async def arun(
4646
"""Async Execute python code inside the CodeBox instance"""
4747

4848
@abstractmethod
49-
def upload(self, file_name: str, content: bytes) -> CodeBoxStatus:
49+
def upload(
50+
self, file_name: str, content: bytes, timeout: int = 600
51+
) -> CodeBoxStatus:
5052
"""Upload a file as bytes to the CodeBox instance"""
5153

5254
@abstractmethod
53-
async def aupload(self, file_name: str, content: bytes) -> CodeBoxStatus:
55+
async def aupload(
56+
self, file_name: str, content: bytes, timeout: int = 600
57+
) -> CodeBoxStatus:
5458
"""Async Upload a file as bytes to the CodeBox instance"""
5559

5660
@abstractmethod

src/codeboxapi/box/codebox.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,23 +190,27 @@ async def arun(
190190
)
191191
)
192192

193-
def upload(self, file_name: str, content: bytes) -> CodeBoxStatus:
193+
def upload(
194+
self, file_name: str, content: bytes, timeout: int = 600
195+
) -> CodeBoxStatus:
194196
return CodeBoxStatus(
195197
**self.codebox_request(
196198
method="POST",
197199
endpoint="/upload",
198200
files={"file": (file_name, content)},
199-
retries=5, # Increase retries for large uploads
200-
backoff_factor=0.5 # Increase backoff factor for large uploads
201+
timeout=timeout,
201202
)
202203
)
203204

204-
async def aupload(self, file_name: str, content: bytes) -> CodeBoxStatus:
205+
async def aupload(
206+
self, file_name: str, content: bytes, timeout: int = 600
207+
) -> CodeBoxStatus:
205208
return CodeBoxStatus(
206209
**await self.acodebox_request(
207210
method="POST",
208211
endpoint="/upload",
209212
files={"file": (file_name, content)},
213+
timeout=timeout,
210214
)
211215
)
212216

src/codeboxapi/box/localbox.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,15 +516,19 @@ async def arun(
516516
print("Error:\n", error)
517517
return CodeBoxOutput(type="error", content=error)
518518

519-
def upload(self, file_name: str, content: bytes) -> CodeBoxStatus:
519+
def upload(
520+
self, file_name: str, content: bytes, timeout: int = 900
521+
) -> CodeBoxStatus:
520522
os.makedirs(".codebox", exist_ok=True)
521523
with open(os.path.join(".codebox", file_name), "wb") as f:
522524
f.write(content)
523525

524526
return CodeBoxStatus(status=f"{file_name} uploaded successfully")
525527

526-
async def aupload(self, file_name: str, content: bytes) -> CodeBoxStatus:
527-
return await asyncio.to_thread(self.upload, file_name, content)
528+
async def aupload(
529+
self, file_name: str, content: bytes, timeout: int = 900
530+
) -> CodeBoxStatus:
531+
return await asyncio.to_thread(self.upload, file_name, content, timeout)
528532

529533
def download(self, file_name: str) -> CodeBoxFile:
530534
with open(os.path.join(".codebox", file_name), "rb") as f:

src/codeboxapi/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Optional
88

99
import requests
10-
from aiohttp import ClientError, ClientResponse, ClientSession, FormData
10+
from aiohttp import ClientError, ClientResponse, ClientSession, ClientTimeout, FormData
1111
from aiohttp.payload import BytesIOPayload
1212

1313
from codeboxapi.config import settings
@@ -106,6 +106,7 @@ def base_request(
106106
endpoint: str,
107107
body: Optional[dict] = None,
108108
files: Optional[dict] = None,
109+
timeout: int = 420,
109110
retries: int = 3,
110111
backoff_factor: float = 0.3,
111112
) -> dict:
@@ -126,7 +127,7 @@ def base_request(
126127
request_data = build_request_data(method, endpoint, body, files)
127128
for attempt in range(retries):
128129
try:
129-
response = requests.request(**request_data, timeout=540)
130+
response = requests.request(**request_data, timeout=timeout)
130131
return handle_response(response)
131132
except requests.RequestException as e:
132133
if attempt < retries - 1:
@@ -143,6 +144,7 @@ async def abase_request(
143144
endpoint: str,
144145
body: Optional[dict] = None,
145146
files: Optional[dict] = None,
147+
timeout: int = 420,
146148
retries: int = 3,
147149
backoff_factor: float = 0.3,
148150
) -> dict:
@@ -180,7 +182,9 @@ async def abase_request(
180182

181183
for attempt in range(retries):
182184
try:
183-
response = await session.request(**request_data)
185+
response = await session.request(
186+
**request_data, timeout=ClientTimeout(total=timeout)
187+
)
184188
return await handle_response_async(response)
185189
except ClientError as e:
186190
if attempt < retries - 1:

0 commit comments

Comments
 (0)