Aiohttp: Create a request object beforehand

Created on 4 Nov 2017  路  3Comments  路  Source: aio-libs/aiohttp

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?

outdated question

Most helpful comment

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())

All 3 comments

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].

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahuigo picture ahuigo  路  5Comments

sersorrel picture sersorrel  路  4Comments

deckar01 picture deckar01  路  4Comments

amsb picture amsb  路  3Comments

jonringer picture jonringer  路  4Comments