如何使异步程序顺序输出?

时间:2019-04-29 07:03:40

标签: python asynchronous aiohttp

我使用python库(aiohttp)获取请求后的响应表单。我可以获得顺序结果吗?怎么...?

import aiohttp
import asyncio
import base64
import json
from tqdm import tqdm
import time



f = open('C:\\Users\\fangchao\\Pictures\\face1.jpg', 'rb').read()
image_64_encode = base64.b64encode(f).decode("utf-8")
files = {"target1": {"photoFormat": "image/jpeg", "photoData": image_64_encode, "faceLocator": None}, "target2": {"photoFormat": "image/jpeg", "photoData": image_64_encode, "faceLocator": None}, "threshold": 0}
url = "http://10.122.100.64:8080/v4/query/verify"


async def fetch(session, url):
    async with session.post(url, data=json.dumps(files)) as response:
        return await response.text()

async def main(url):
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, url)
        text = json.loads(html)
        print(text["score"])


a = time.time()
loop = asyncio.get_event_loop()
tasks = [main(url) for i in range(1000)]
loop.run_until_complete(asyncio.wait(tasks))
print(time.time() - a)

我只能获得分数表答复,因此如果我使用很多帖子来获得答复,我想知道两张图片是什么。所以我需要一个顺序的结果

0 个答案:

没有答案