Request Timeouts
In the next three lessons you’ll have a closer look at performance of requests. Therefore, the following three things are covered:
- Timeouts
- The Session Object
- Max Retries
This lesson covers Timeouts.
00:00
This next part is all about performance, keeping your application running smoothly. Performance can be broken up into timeouts, the Session
object, and max retries. Let’s start with timeouts.
00:11
The timeout
is a parameter of how long we’re going to wait for a response before moving on. The default for requests
is to wait indefinitely. With the timeout
parameter, you can set an amount of time in seconds, and that value can be either an integer or a float. A tuple could be used—the first value would be how long to establish the connection, and the second would be how long to wait for the response. Let’s check it out.
00:34
I’m back here in a REPL. I need to make sure I import requests
. To set the timeout
parameter… actually, let’s set our url
again, save us some time.
00:49
Now that we have that variable, we can use it. Let’s try a GET
request to the Root Rest API again. And in this particular case, the parameter we’re going to put in is the timeout
.
01:00
Let’s just set it to 1
second and see what that does. All right! Successful. We waited up to a second and the moment it had the response, it no longer was waiting. What if we set something too low?
01:13
If we create a timeout
that’s much too low—like, let’s say one thousandth of a second—what happens? So, we’re going to be super impatient here. In this case… it caused a ConnectTimeoutError
. The connection to api.github.com
timed out with this very low timeout
value, so it raised an exception—the ConnectTimeout
exception. Let’s try it again.
01:44
Let’s go with a hundredth of a second. Oh, same. Too impatient. How about just a tenth of a second? Okay. So, how about half a second? Okay. So, in this particular case with my current internet connection and what’s going on, a half second seems be a long enough timeout
.
02:08
You may want to leave a little bit more—2
seconds might be a good value. As you can see, it didn’t wait two seconds to finish it—it’s just, “When will it time out?” So in the intro, I talked about setting up a tuple to designate the amount of time that we’re going to establish the connection, and the second is to wait for the response.
02:31
Let’s try this—2
seconds here and 5
seconds there… and it looks like we’re successful. If either of these is set too low, we will get an error. Let’s see what that looks like.
02:47
So, we’ll wait 2
seconds for the connection, but for the response we’ll wait 0.1
of a second.
02:56
So, it gave us a different exception in this case, a ReadTimeout
. So we were able to establish the connection there, but unable to wait for the amount of time for it to read our response back.
03:10
Let’s try… we can set it to something like 5
seconds, or we can even set it to 1
second. Oops. I’d better close my request. It looks like that was successful.
03:27 So, this is something that’s going to require some tuning depending on the API that you’re talking to.
03:34 That’s why we’re talking about performance.
03:38
Ope, got to spell requests
. There we go. Success. For these errors, instead of getting this very verbose error here, we could also set up a section in our code to print out something that’s a little more readable in the console. Let’s try that out.
03:57 So, we’re going to create a new file, a new script. I’m going to resize my window a little bit.
04:05
For this file, I’m going to name it timeout.py
. Exit my REPL. So, for the script we’ll import requests
first, and then from requests.exceptions
we’ll import Timeout
. We’ll set up a try
statement.
04:26
Let me hide my files there. There we go—a little more screen real estate. And we’ll set our timeout
here for just 1
second. And our except
will be for a Timeout
except.
04:44
In that particular case, we’ll have it print to the console that the request did time out. And if successful, else
we’ll print 'The request did not time out'
.
05:00 So, we can save that. Down here, we can run it.
05:11
Great! So if we set it again too low, what would that look like? Let’s say we set it to 0.1
of a second—something too impatient—and we save, run it again.
05:23
Okay. In that case, it did time out. We didn’t wait a long enough time. So, in this case, maybe we set it to 3
seconds, so we’ll be fairly patient here.
05:39
It didn’t wait a full three seconds to get the response back. It’s just, “When will it time out? What is the limit we’re willing to wait?” Okay! Next, let’s talk about the Session
object.
Become a Member to join the conversation.