我有一系列异步请求对,其中一对由请求 A 和请求 B 组成。此外,请求 B 依赖于请求 A。换句话说,我需要将数据从响应 A 传递到请求 B。因此,我需要安排任务,使得每个任务发送请求 A,然后仅在响应 A 返回后发送请求 B。
from aiohttp import ClientSession
from typing import *
import asyncio
async def request_A(url: str, session: ClientSession) -> dict:
async with session.request('get', url) as response:
return await response.json()
async def request_B(url: str, data: dict, session: ClientSession) -> dict:
async with session.request('post', url, json=data) as response:
return await response.json()
async def request_chain(url_A: str, url_B: str, session: ClientSession) -> dict:
response_A_data = await request_A(url_A, session)
response_B_data = await …
Run Code Online (Sandbox Code Playgroud)