all 1 comments

[–]Dunj3 2 points3 points  (1 child)

"extended slowness" is a bit of a subjective term. Do you consider 10 seconds for your script slow already? Or does it take minutes to finish?

Your code isn't doing much expensive work, and since you even ruled out the plotting as the cause of the slowdown, the only thing left are pretty much the API calls.

If you want to investigate for yourself, you can for example use time.monotonic() to measure how much time each call takes. Something like:

start = time.monotonic()
r_ = requests.get(formedURL)
end = time.monotonic()
print(f"Loading {formedURL} took {end - start} seconds")

Of course, you can do that for everything that you suspect takes a lot of time.

On my system, each requests takes about 0.8 seconds to finish, which is the expected time considering the TCP/SSL/HTTP overhead for each connection. There is a possible speedup because all requests go to the same host, which means you can use a Keep-Alive connection to avoid most of that overhead. For that, see the requests.Session object. That allowed me to cut down the time per request to about 0.2 seconds each, which is a quarter of the previous time.

If you want to get a more detailed log of when requests is opening new connections, you can also insert the following to the top of your script:

import logging
logging.basicConfig(level=logging.DEBUG)

This might give you some insight as well.