How to call in a async mode? #523
Answered
by
kemingy
decadance-dance
asked this question in
Q&A
-
I am trying to send multiple requests in parallel without waiting responses, but I can't manage this. My code look like this: from http import HTTPStatus
import time
import base64
import cv2
import asyncio
import httpx
import msgpack # type: ignore
import numpy as np
import numpy.typing as npt
import pickle
from tqdm import tqdm
dog_bytes = httpx.get(
"https://raw.githubusercontent.com/pytorch/hub/master/images/dog.jpg"
).content
async def async_call(n_repeat: int):
url = "http://127.0.0.1:8000/inference"
results = []
async with httpx.AsyncClient() as client:
for i in tqdm(range(n_repeat)):
prediction = await client.post(
url,
content=msgpack.packb({"data": dog_bytes}),
)
results.append(prediction)
if prediction.status_code != HTTPStatus.OK:
raise Exception(prediction.content)
return results
def sync_call(n_repeat: int):
url = "http://127.0.0.1:8000/inference"
results = []
for i in tqdm(range(n_repeat)):
prediction = httpx.post(
url,
content=msgpack.packb({"data": dog_bytes}),
)
results.append(prediction)
if prediction.status_code != HTTPStatus.OK:
raise Exception(prediction.content)
return results
s = time.time()
asyncio.run(async_call(50))
print(time.time()-s)
s = time.time()
sync_call(50)
print(time.time()-s) I expect to get something like coroutine objects to read them when I want it. |
Beta Was this translation helpful? Give feedback.
Answered by
kemingy
May 2, 2024
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
decadance-dance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
await
the async function one-by-one makes it work like a sync function call. You can useasyncio.gather
. Or you can also use the ThreadPool, ref to https://github.com/mosecorg/mosec/blob/main/tests/bad_req.py