diff --git a/run.py b/run.py index 051f863..3812571 100644 --- a/run.py +++ b/run.py @@ -527,8 +527,17 @@ def clean_websocket(function): def chunks(l, n): """Yield successive n-sized chunks from l.""" - for i in range(0, len(l), n): - yield l[i:i + n] + chunk = [] + a = 0 + + for i in l: + if a < n: + a += 1 + chunk.append(i) + else: + yield chunk + chunk = [] + a = 0 @app.websocket('/index-ws') @@ -556,9 +565,9 @@ async def ws_index(request, websocket): .order_by(-Job.id) # chunks initial data by batch of 30 to avoid killing firefox - data = chunks(list(itertools.chain(map(model_to_dict, next_scheduled_jobs), - map(model_to_dict, Job.select().where(Job.state == "running")), - map(model_to_dict, latest_done_jobs))), 30) + data = chunks(itertools.chain(map(model_to_dict, next_scheduled_jobs), + map(model_to_dict, Job.select().where(Job.state == "running")), + map(model_to_dict, latest_done_jobs)), 30) await websocket.send(ujson.dumps({ "action": "init_jobs",