Skip to content

Instantly share code, notes, and snippets.

@victoraugustolls
Created March 2, 2020 15:17
Show Gist options
  • Save victoraugustolls/01002ae218366b453e962446c9c8a274 to your computer and use it in GitHub Desktop.
Save victoraugustolls/01002ae218366b453e962446c9c8a274 to your computer and use it in GitHub Desktop.
import asyncio
import httpx
async def do_request(client: httpx.AsyncClient):
try:
await client.get(
url="https://google.com",
)
except Exception as e:
print(f"{type(e)}: {str(e)}")
async def request_multiple(client: httpx.AsyncClient):
count = 0
while True:
print(f"Request number: {count}")
await asyncio.gather(
do_request(client=client),
do_request(client=client),
do_request(client=client),
do_request(client=client),
do_request(client=client)
)
count += 5
if __name__ == "__main__":
cl = httpx.AsyncClient()
asyncio.run(request_multiple(client=cl))
@victoraugustolls
Copy link
Author

Output with HTTP/2 disabled:

Request number: 0
Request number: 5
Request number: 10
Request number: 15
Request number: 20
Request number: 25
Request number: 30
Request number: 35
Request number: 40
Request number: 45
Request number: 50

@victoraugustolls
Copy link
Author

Output with HTTP/2 enabled:

Request number: 0
Request number: 5
<class 'httpx.exceptions.ReadTimeout'>: 
<class 'httpx.exceptions.ReadTimeout'>: 
<class 'KeyError'>: HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))
<class 'KeyError'>: HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))
Request number: 10
Request number: 15
<class 'httpx.exceptions.ReadTimeout'>: 
<class 'KeyError'>: HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))
<class 'KeyError'>: HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))
<class 'KeyError'>: HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))
Request number: 20
Request number: 25
<class 'httpx.exceptions.ReadTimeout'>: 
<class 'httpx.exceptions.ReadTimeout'>: 
<class 'KeyError'>: HTTPConnection(origin=Origin(scheme='https' host='google.com' port=443))
Request number: 30

@victoraugustolls
Copy link
Author

Modified to run with httpxprof:

import asyncio

import httpx
import httpxprof


async def do_request(client: httpx.AsyncClient):
    try:
        await client.get(
            url="https://google.com",
        )
    except Exception as e:
        print(f"{type(e)}: {str(e)}")


async def request_multiple(client: httpx.AsyncClient):
    count = 0

    while count < 2:
        print("Count:", count)
        await asyncio.gather(
            do_request(client=client),
            do_request(client=client),
            do_request(client=client),
            do_request(client=client),
            do_request(client=client),
            do_request(client=client),
            do_request(client=client),
            do_request(client=client),
            do_request(client=client),
            do_request(client=client)
        )
        count += 1
    print("Finished")


async def main(config: httpxprof.Config) -> None:
    cl = httpx.AsyncClient(http2=False)
    await request_multiple(client=cl)

@victoraugustolls
Copy link
Author

With http2 enabled:

image

@victoraugustolls
Copy link
Author

With http2 disabled:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment