Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit ec5ae47

Browse files
committed
abort BulkCreate after 50s to avoid ServerTimeout
1 parent 0f040c2 commit ec5ae47

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

flask_mongorest/views.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,22 @@ def post(self, **kwargs):
271271
})
272272
raw_data_deque = deque(raw_data)
273273
self._resource.view_method = methods.BulkCreate
274-
ret = []
274+
data = []
275+
tic = time.perf_counter()
275276
while len(raw_data_deque):
276277
self._resource._raw_data = raw_data_deque.popleft()
277-
ret.append(self.create_object())
278-
return {'data': ret, 'count': len(ret)}, '201 Created'
278+
data.append(self.create_object())
279+
dt = time.perf_counter() - tic
280+
if dt > 50:
281+
break
282+
283+
count = len(data)
284+
msg = f"Created {count} objects in {dt:0.1f}s ({count/dt:0.3f}/s)."
285+
print(msg)
286+
ret = {'data': data, 'count': count}
287+
if raw_data_deque:
288+
ret['warning'] = f"{msg} Remaining objects in batch skipped to avoid Server Timeout."
289+
return ret, '201 Created'
279290
else:
280291
raise ValidationError({'error': 'wrong payload type'})
281292

0 commit comments

Comments
 (0)