【python】接口测试中的异步调用
2021-04-22 15:26
标签:on() json print end code style request for res 接口测试中可以可以使用httpx进行异步调用,下面对比一下异步调用和同步调用的速度 异步调用的代码如下 结果为 同步调用代码如下 结果为: 【python】接口测试中的异步调用 标签:on() json print end code style request for res 原文地址:https://www.cnblogs.com/ffrs/p/13276799.html# 异步调用
import httpx
import asyncio
import time
async def request(client):
resp = await client.get(‘http://httpbin.org/get‘)
result = resp.json()
async def main():
async with httpx.AsyncClient() as client:
# 100 次调用
task_list = []
for _ in range(100):
req = request(client)
task = asyncio.create_task(req)
task_list.append(task)
await asyncio.gather(*task_list)
if __name__ == "__main__":
#开始
start = time.time()
asyncio.run(main())
# 结束
end = time.time()
print(f‘异步:发送100次请求,耗时:{end - start}‘)
# 同步调用
import time
import httpx
def make_request(client):
resp = client.get(‘http://httpbin.org/get‘)
result = resp.json()
def main():
session = httpx.Client()
# 1000 次调用
for _ in range(100):
make_request(session)
if __name__ == ‘__main__‘:
# 开始
start = time.time()
main()
# 结束
end = time.time()
print(f‘同步:发送100次请求,耗时:{end - start}‘)