Created
March 2, 2020 15:17
-
-
Save victoraugustolls/01002ae218366b453e962446c9c8a274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
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
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)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output with HTTP/2 disabled: