0

I'm trying http requests using python httpx client. the problem is every time I make requests the session object not reusing the connection. so it creating the connection every time and consuming time there and ultimately reducing the performance. here i only have to do one requests at a time i.e. after interval of 5 sec. the code is below.

import requests
import httpx
import json
import cProfile
import time


Limits = httpx.Limits(max_connections=1, max_keepalive_connections=1,
                      keepalive_expiry=60)
s = requests.session()
hclient = httpx.Client(limits=Limits)

def get_requests():
    t1 = time.time()
    res = hclient.get('https://shoonya.finvasia.com')
    t2 = time.time()
    total_time = t2 - t1
    print(f'Time Taken to complete the requests is {total_time}s.')

def check_performance():
    cProfile.run("get_requests()")
    cProfile.run("get_requests()")

#checking with timing waiting 3 seconds and measuring requests time.
# requests placed very next moment is not taking time. latency less than 100ms.
time.sleep(3)
get_requests()
get_requests()

can anyone tell me what is wrong here.

  • Where did you see this behaviour of httpx not trusting connection? Running your code as is shows that it's reusing connections. – pepoluan Nov 18 '22 at 03:42
  • somehow connection is breaking and client itself reestablishing connection. i checked using cProfile. – Rahul Ghuge Nov 21 '22 at 02:42

0 Answers0