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

ابزارهای یادگیری هوش مصنوعی را باز کنید

ثبت‌نام کنید تا به ابزارهای قدرتمندی دسترسی پیدا کنید که به شما کمک می‌کنند سریع‌تر از هر ویدیو یاد بگیرید.

توضیح صحنه شکارچی عبارات مرور فلش‌کارت تمرین تکرار مکالمه تعاملی
ثبت‌نام رایگان
انگلیسی 24:02 Science & Tech

Retry Pattern: The Secret to Resilient Python Code

Late Night with Seth Meyers · 27,806 بازدید · اضافه شده 4 روز پیش

زیرنویس‌ها (560 بخش‌ها)

00:00

Here's an example of piece of code that

00:02

calls the Chuck Norris API to retrieve a

00:05

random joke. Let's run this and see what

00:07

happens. Oops, that's a crash. Let's try

00:10

this again. Ah, and now it works. I

00:14

didn't change anything. So clearly, if

00:16

there is some temporary issue with the

00:18

API that you're connecting to, the whole

00:20

script crashes. Now, of course, this is

00:22

a bad example. Actually, this API never

00:25

fails because, well, it's the Chuck

00:27

Norris API. Merely suggesting the

00:30

possibility probably means that this is

00:32

my last video ever. Anyway, today I'll

00:35

show you how to stop your code from

00:37

crashing randomly whenever you're

00:38

connecting with APIs or other services

00:41

by using the retry pattern. I'll go step

00:44

by step and show you simple retries,

00:46

exponential backoff, decorators,

00:48

fallbacks, and more. This video is

00:51

sponsored by SER API. More about that

00:53

later. The problem that I encountered at

00:55

the start of the video is so-called

00:56

transient failure. Most modern systems

01:00

rely on a bunch of external services,

01:03

APIs, databases, large language models,

01:06

you name it. And these services

01:07

sometimes fail briefly, even when your

01:10

code is correct. Now, the retry pattern

01:13

wraps an operation that might fail and

01:16

automatically retries it before giving

01:19

up. It's actually a really simple way to

01:21

make your code more robust and fault

01:24

tolerant. And by the way, maybe you

01:25

noticed I had this little wound here.

01:28

Actually, what happened is that I was in

01:30

a fight to the death with Chuck Norris.

01:33

I'm really attempting fate here. Anyway,

01:36

while I'm still alive, let's build a

01:38

simple retry function ourselves. And as

01:41

you can see, what I secretly added to

01:43

the example is a random failure because,

01:46

like I said, the Chug Norris API never

01:48

fails. So let's create a function called

01:51

retry

01:53

and this function is going to get an

01:55

operation which is going to be a

01:57

callable. So it will be a function and

01:59

it will call this function up to three

02:01

times and we want it to wait let's say 1

02:04

second between the attempts.

02:07

So from typing I'm going to import the

02:09

callable type

02:12

and let's assume this operation doesn't

02:15

take any arguments because otherwise we

02:16

don't know how to call it. And well, we

02:18

could make the return type any but

02:21

instead what we can also do is make

02:23

retry a generic function. So it's going

02:26

to return something of type T. So that's

02:28

the operation. Then we have the number

02:31

of retries that we want to have and

02:34

let's say that's by default it's three

02:37

and we'll also have a delay

02:40

in seconds. So let's say that's going to

02:43

be 1 second by default. And then this is

02:46

going to return a type t because that's

02:48

the result of the operation. So for

02:52

attempts in range let's say from one to

02:56

retries + one.

03:03

We're going to try to return the call of

03:06

the operation like so. And then if there

03:09

is some exception

03:12

and this is actually one of the rare

03:14

cases where you want to basically catch

03:16

everything because we're actually going

03:18

to reerase this if we can't retry

03:20

anymore. So if uh attempts

03:25

equals retries, that means we have now

03:29

had the maximum number of retries. We're

03:31

going to erase the exception. Otherwise,

03:36

we're going to do times sleep the delay.

03:39

And obviously, we're going to need to

03:42

import time like so. And perhaps

03:49

let's print some logging.

03:57

There we go. So this is our simple

04:00

pretty naive uh retry function. And then

04:03

what we can do here is we have a joke

04:06

and that equals retry.

04:10

We will retry fetch joke and we'll leave

04:13

everything else as the default value. So

04:15

three times and one second delay. And

04:18

then we're going to print the joke

04:20

like so. Let's run this and see what

04:23

happens.

04:25

So immediately we get a good result.

04:28

Let's try that again.

04:32

Ah, there we go. We got two failed

04:34

attempts and then finally the after

04:36

three attempts it doesn't work anymore.

04:38

So, as you see, it retries it until it

04:41

reaches that end point. And as you can

04:43

see, this doesn't always work. In some

04:45

cases, it still fails because we set the

04:48

maximum number of retries to three. But

04:52

also, sometimes it actually helps. Like

04:54

in this case, for example, the first

04:56

time it failed, but then the second time

04:57

it succeeded. So, it's a small change. I

05:00

mean, this is pretty easy to add to your

05:02

code, right? especially if you turn this

زیرنویس کامل در پخش‌کننده ویدیو موجود است

با تمرین‌ها یاد بگیرید

تمرین‌های واژگان، گرامر و درک مطلب از این ویدیو بسازید

واژگان و گرامر آزمون درک مطلب آزمون IELTS تمرین نوشتاری
ثبت‌نام برای تمرین
هنوز نظری وجود ندارد. اولین نفری باشید که افکار خود را به اشتراک می‌گذارد!

ثبت‌نام کن و همه امکانات رو باز کن

پیشرفتت رو دنبال کن، واژگان رو ذخیره کن و تمرین کن

شروع رایگان یادگیری زبان