File size: 1,442 Bytes
9b8bd50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""https://zetcode.com/python/concurrent-http-requests/"""


import asyncio
import random
import time

import httpx

local_url = "http://127.0.0.1:5000"
remote_url = "https://cetinca-mathtext-nlu.hf.space/run/text2int_preprocessed"
remote_url = "https://tangibleai-mathtext.hf.space/run/"
headers = {"Content-Type": "application/json; charset=utf-8"}
data_list = [
    ["one hundred forty five"],
    ["ninety nine"],
    ["seventy three"],
    ["one thousand seven hundred sixty nine"],
]


# async call to endpoint
async def call_api(url, _data_list, number):
    data = random.choice(_data_list)
    json = {"data": data, "fn_index": 1}
    async with httpx.AsyncClient() as client:
        begin1 = time.time()
        begin2 = time.perf_counter()  # Used perf_counter for more precise result.
        # print(f"Call {number} started on: {start} text: {data}")
        response = await client.post(url=url, headers=headers, json=json, timeout=30)
        print(response.status_code)
        end1 = time.time()
        end2 = time.perf_counter()
        return f"Call_{number}: start: {begin1} end: {end1} delay: {end1 - begin1} with_time: {end2 - begin2}\n"\
               f"input_text: {data} result: {response.json().get('data')}"


async def main(n):
    calls = []
    for num in range(n):
        calls.append(call_api(remote_url, data_list, num))
    r = await asyncio.gather(*calls)
    print(*r, sep="\n")


asyncio.run(main(1))