Browse Source

Асинхронный запрос с ограничем и offset

master
Дмитрий 3 years ago
parent
commit
23938fd5e4
  1. 22
      server.py
  2. 43
      solution.py

22
server.py

@ -15,12 +15,13 @@ curl --request GET \
from flask import Flask, jsonify, request
import json
import secrets
import random
app = Flask(__name__)
@app.route("/api/portal/get", methods=["GET"])
def index():
def get():
"""
Единственная функция сервера-пирожочка.
@ -46,4 +47,23 @@ def index():
return jsonify({"result": hosts, "done": True})
@app.route("/api/portal/count", methods=["GET"])
def count():
"""
Единственная функция сервера-пирожочка.
Вернет json, в формате:
{
"result": 100,
"done": true
}
В этой реализации, принимает аргумент `limit=N`, N -- количество
генерируемых значений, а не выборка.
"""
# print(request)
return jsonify({"result": random.randrange(1, 300), "done": True})
app.run()

43
solution.py

@ -38,22 +38,53 @@ def _get_template(templ_file: str) -> str:
return template
async def get_records_count() -> int:
async with aiohttp.ClientSession() as session:
pass
async def get_records_count(session, server) -> int:
"""Возвращает количество записей в базе."""
async with session.get(f"{server}/count") as resp:
r = await resp.json()
count: int = int(r["result"])
return count
async def get_tasks(session, server, count, body, request_portion) -> list:
"""Вернет список запросов к API.
Функция не ограничивает кол-во запросов, это нужно сделать до
вызова, чтобы передать корректный `session`.
"""
tasks = []
offset = 0
for r in range(count // request_portion + 1):
if offset < count:
print(f"{offset=}")
tasks.append(asyncio.create_task(session.get(f"{server}/get", json=body)))
offset += request_portion
body["offset"] = offset
print(f"{count=}")
return tasks
# изменить сигнатуру на (cfg: Conifg, json_body: dict)
async def send_async_request(cfg: dict, columns: list, limit: int = 1) -> None:
body = {"columns": columns, "limit": 1}
request_portion = 10
requests_count = 10
server = cfg["central_host_url"]
path_to_template_file = cfg["template"]
path_for_config = cfg["path_for_config"]
template: str = _get_template(path_to_template_file)
async with aiohttp.ClientSession() as session:
tasks = [session.get(server, json=body) for i in range(limit)]
# ограничим одновременное число запросов
conn = aiohttp.TCPConnector(limit=requests_count)
async with aiohttp.ClientSession(connector=conn) as session:
# всего записей в базе
count = await get_records_count(session, server)
body = body = {"columns": columns, "limit": request_portion, "offset": 0}
tasks = await get_tasks(session, server, count, body, request_portion)
responses = await asyncio.gather(*tasks)
for response in responses:

Loading…
Cancel
Save