There is my code down below. I don't understand one thing. Since I removed await
keyword for both below lines:
response1 = await session.post("https://a")
response2 = await session.post("https://b")
I expected to see in output immediately :
Requesting response1...
Requesting response2...
Nevertheless i only see when program runs:
Requesting response1..
.
While its processing request1
nevertheless to me second text should be printed. Why it works like that?
My code:
import asyncio
from pip._vendor import requests
async def getrequest():
session = requests.Session()
try:
print("Requesting response1...")
response1 = session.post("https://a") # SEE > i am not awaiting here
print("Requesting response2...")
response2 = session.post("https://b") # SEE > i am not awaiting here
await response1;
await response2;
print("Getting response ...")
print(f"Code status is: {response1.status_code}")
print(response1.json())
print("Done")
except Exception as err:
print(f"An error ocurred:\n {err}")
async def main():
await asyncio.gather(getrequest())
print("Sleeping")
await asyncio.sleep(10)
print("2")
asyncio.run(main())