Ask Your Question
1

How can I make Python Requests not wait for the request to be completed?

asked 2023-06-28 14:51:20 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-28 15:11:02 +0000

djk gravatar image

You can use the asynchronous requests library like aiohttp to make requests without waiting for them to be completed.

Here's an example:

import aiohttp
import asyncio

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        # send request without waiting for response
        session.get('http://example.com')

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

In this example, we use aiohttp to create an asynchronous session and then issue a request using the session.get() method without waiting for the response. The event loop will continue to run other tasks while the request is being sent.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-28 14:51:20 +0000

Seen: 8 times

Last updated: Jun 28 '23