Does python have asynchronous web client requests (like C# async/await)? Or is each call fundamentally synchronous (like Java 1.8)?

Question:

Im writing a Python client for a restful service so that I can make requests within a python application. There already exists a C# and a Java client and Im trying to figure out which of the two clients I should follow as a guide. So does python have asynchronous web client requests (like C# async/await)? Or is each call fundamentally synchronous (like Java 1.8)? I am not asking for a recommendation for a software library, programming language, etc. What I’m asking is if the python runtime has a web client class that can make async requests like C#. Or if it’s like Java 1.8 where all it has is the synchronous calls.

Asked By: zaidabuhijleh

||

Answers:

you can use the Asyncio library

import asyncio

async def wait_around(n, name):
    for i in range(n):
        print(f"{name}: iteration {i}")
        await asyncio.sleep(1.0)

async def main():
    await asyncio.gather(*[
        wait_around(2, "coroutine 0"), wait_around(5, "coroutine 1")
    ])

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Answered By: Michiel

What I’m asking is if the python runtime has a web client class that can make async requests

No, the runtime itself does not have support for async web requests, and using asynchio as per the previous answer doesn’t work for web requests.

I know you said you weren’t looking for library recommendations, but for completeness sake, the only way I’ve seen to make async web requests from python is by using the
aiohttp package.

Answered By: rborchert
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.