[파이썬] requests 비동기 요청 처리

In the world of web development, it is often necessary to make multiple HTTP requests simultaneously or concurrently, to improve performance and reduce the overall response time. Asynchronous programming allows us to achieve this by executing tasks concurrently, without blocking the execution of the main program.

In this blog post, we will explore how to handle asynchronous HTTP requests using the requests-async library in Python. It is an extension of the popular requests library, which enables us to make asynchronous HTTP requests instead of blocking ones.

Installing requests-async

The first step is to install the requests-async library. You can install it using pip by running the following command in your terminal:

pip install requests-async

Make sure you have python3.7 or higher installed in your environment to use this library.

Making asynchronous requests

Let’s dive into the code and see how to make asynchronous HTTP requests using the requests-async library.

import asyncio
from requests_async import AsyncSession

async def make_requests(urls):
    async with AsyncSession() as session:
        tasks = [session.get(url) for url in urls]
        responses = await asyncio.gather(*tasks)
        for response in responses:
            print(response.text)

urls = ['https://example.com', 'https://google.com', 'https://github.com']

# Run the asynchronous requests
loop = asyncio.get_event_loop()
loop.run_until_complete(make_requests(urls))

In the above code, we first import the necessary modules. Then, we define an async function called make_requests.

Inside this function, we create an AsyncSession object, which is the asynchronous equivalent of the requests.Session object. We then create a list of tasks by calling session.get for each URL.

Next, we use asyncio.gather to wait for all the tasks to complete and collect the responses. Finally, we iterate over the responses and print the respective response texts.

Don’t forget to replace the urls list with the actual URLs you want to make requests to.

Conclusion

Using the requests-async library, we can easily make asynchronous HTTP requests in Python. This allows us to handle multiple requests concurrently, improving the performance of our web applications. Asynchronous programming is particularly useful when dealing with slow or unresponsive APIs, scraping large amounts of data, or any scenario where we need to perform multiple requests efficiently.

Give it a try in your next project and experience the power of asynchronous programming in Python!