The video owner has disabled playback on external websites.
This video is no longer available on YouTube.
This video cannot be played right now.
Watch on YouTube
Desbloqueie ferramentas de aprendizado com IA
Cadastre-se para acessar ferramentas poderosas que ajudam a aprender mais rápido com cada vídeo.
Retry Pattern: The Secret to Resilient Python Code
Legendas (560 segmentos)
Here's an example of piece of code that
calls the Chuck Norris API to retrieve a
random joke. Let's run this and see what
happens. Oops, that's a crash. Let's try
this again. Ah, and now it works. I
didn't change anything. So clearly, if
there is some temporary issue with the
API that you're connecting to, the whole
script crashes. Now, of course, this is
a bad example. Actually, this API never
fails because, well, it's the Chuck
Norris API. Merely suggesting the
possibility probably means that this is
my last video ever. Anyway, today I'll
show you how to stop your code from
crashing randomly whenever you're
connecting with APIs or other services
by using the retry pattern. I'll go step
by step and show you simple retries,
exponential backoff, decorators,
fallbacks, and more. This video is
sponsored by SER API. More about that
later. The problem that I encountered at
the start of the video is so-called
transient failure. Most modern systems
rely on a bunch of external services,
APIs, databases, large language models,
you name it. And these services
sometimes fail briefly, even when your
code is correct. Now, the retry pattern
wraps an operation that might fail and
automatically retries it before giving
up. It's actually a really simple way to
make your code more robust and fault
tolerant. And by the way, maybe you
noticed I had this little wound here.
Actually, what happened is that I was in
a fight to the death with Chuck Norris.
I'm really attempting fate here. Anyway,
while I'm still alive, let's build a
simple retry function ourselves. And as
you can see, what I secretly added to
the example is a random failure because,
like I said, the Chug Norris API never
fails. So let's create a function called
retry
and this function is going to get an
operation which is going to be a
callable. So it will be a function and
it will call this function up to three
times and we want it to wait let's say 1
second between the attempts.
So from typing I'm going to import the
callable type
and let's assume this operation doesn't
take any arguments because otherwise we
don't know how to call it. And well, we
could make the return type any but
instead what we can also do is make
retry a generic function. So it's going
to return something of type T. So that's
the operation. Then we have the number
of retries that we want to have and
let's say that's by default it's three
and we'll also have a delay
in seconds. So let's say that's going to
be 1 second by default. And then this is
going to return a type t because that's
the result of the operation. So for
attempts in range let's say from one to
retries + one.
We're going to try to return the call of
the operation like so. And then if there
is some exception
and this is actually one of the rare
cases where you want to basically catch
everything because we're actually going
to reerase this if we can't retry
anymore. So if uh attempts
equals retries, that means we have now
had the maximum number of retries. We're
going to erase the exception. Otherwise,
we're going to do times sleep the delay.
And obviously, we're going to need to
import time like so. And perhaps
let's print some logging.
There we go. So this is our simple
pretty naive uh retry function. And then
what we can do here is we have a joke
and that equals retry.
We will retry fetch joke and we'll leave
everything else as the default value. So
three times and one second delay. And
then we're going to print the joke
like so. Let's run this and see what
happens.
So immediately we get a good result.
Let's try that again.
Ah, there we go. We got two failed
attempts and then finally the after
three attempts it doesn't work anymore.
So, as you see, it retries it until it
reaches that end point. And as you can
see, this doesn't always work. In some
cases, it still fails because we set the
maximum number of retries to three. But
also, sometimes it actually helps. Like
in this case, for example, the first
time it failed, but then the second time
it succeeded. So, it's a small change. I
mean, this is pretty easy to add to your
code, right? especially if you turn this
Legendas completas disponíveis no player de vídeo
Pratique com exercícios
Gere exercícios de vocabulário, gramática e compreensão deste vídeo
Comentários (0)
Faça Login para ComentarCadastre-se para desbloquear todos os recursos
Acompanhe seu progresso, salve vocabulário e pratique com exercícios
Modo interativo
Questionário
Resposta certa:
Questionário
Resposta certa:
Os quizzes aparecem enquanto você assiste ao vídeo
Dica de memorização
Deste vídeo
Aprenda idiomas de grátis