I have a question regarding the system.net.httpClient() function. I have a script that loops through a list of API tag IDs and queries an API for the latest data from each tag. I noticed it was fairly slow (about 4 seconds per tag). My original code creates a client instance and then reuses it for each tag in the loop:
client = system.net.httpClient(bypass_cert_validation=True, cookie_policy = "ACCEPT_ALL")
for tagID in tags:
url = "%s/sources/%s/values?apiKey=%s&start_date=%s&end_date=%s&lang=en" % (host_url, tagID, apiKey, start, end)
client.get(url=url)
But when I was playing around with it, I noticed it was much faster (0.3 seconds per tag) if I created a new client instance each time:
for tagID in tags:
client = system.net.httpClient(bypass_cert_validation=True, cookie_policy = "ACCEPT_ALL")
url = "%s/sources/%s/values?apiKey=%s&start_date=%s&end_date=%s&lang=en" % (host_url, tagID, apiKey, start, end)
client.get(url=url)
I noticed the manual says this is not recommended, but I don't think it will be fast enough using the first method. Is there a major concern with the second method? Is there something I am missing for why the first method is so slow?
Excerpt from manual:
Be aware that httpClient instances are heavyweight, so they should be created sparingly and reused as much as possible. For ease of reuse, consider instantiating a new httpClient as a top-level variable in a project library script.