Hello!
I have such a question - for now i user requests library and want to switch to using aiohttp, can you please tell me with request my program works like this:
session = requests.Session()
requests_to_fetch = []
for url,data,json,method in some_iterable:
requests_to_fetch.append(
requests.Request(method=method, url=url, data=data, json=json)
)
...
for request in requests_to_fetch:
response = session.send(session.prepare_request(request))
...
But in aiohttp i cant find something like Request object which i can made before making actual request. Is where some class to do so?
import asyncio
import aiohttp
async def main():
async with aiohttp.ClientSession() as session:
requests_to_fetch = []
for url,data,json,method in some_iterable:
# data=data, json=json - try to take a look at docs how to send both of them
request = session.request(method, url)
requests_to_fetch.append(request)
responses = await asyncio.gather(*requests_to_fetch)
htmls = await asyncio.gather(*[response.read() for response in responses])
[response.release() for response in responses]
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
@Smosker I'm closing this question as answered. Feel free to reopen it if you have additional questions on the topic.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
Most helpful comment