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

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

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

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

The Lazy Loading Pattern: How to Make Python Programs Feel Instant

Late Night with Seth Meyers · 41,598 بازدید · اضافه شده 3 روز پیش

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

00:00

I have a script here that loads data

00:01

from a pretty large CSV file. As you can

00:05

see, there's like millions of records in

00:08

here. Now, I'm doing this in a pretty

00:11

naive way. I'm simply loading it here.

00:13

And then I have some functions to

00:15

analyze sales, count the number of

00:17

sales, and there's a simple textbased

00:19

user interface.

00:22

This is what happens when I run this

00:24

code. So, as you can see, it loads all

00:26

that CSV data, and that actually takes a

00:29

pretty long time. So, the program is not

00:32

frozen. It's working, but it's loading

00:35

these millions of rows before doing

00:37

actually anything useful. This is a

00:39

classic example of eager loading. But

00:42

today, I'm going to fix this by using

00:44

the lazy loading pattern and show you a

00:46

couple of things that you can do.

00:48

Actually, the concept of lazy loading

00:50

dates back to already 1960s when

00:53

operating systems began loading memory

00:56

pages only when accessed. Martin Fowler

00:59

later popularized it in patterns of

01:02

enterprise application architecture,

01:04

describing how OMS like Django delay

01:07

database queries until the data is

01:09

actually used. And you probably also use

01:11

this daily yourselves by using uh

01:14

Django, SQL Alchemy, TensorFlow and even

01:17

Pandas rely on it to handle large data

01:20

more efficiently. And there's many other

01:22

real life examples of lazy loading as

01:24

well in uh websites that already show

01:27

the UI before the data is actually there

01:29

or operating systems that show you the

01:31

login screen while it's still in the

01:34

background loading all sorts of stuff.

01:36

So let's see how you can use that same

01:38

principle in Python code. Now before we

01:41

start, if you want to learn more about

01:42

how to design a piece of software from

01:44

scratch, I have a free guide for you at

01:46

iron.codes/design

01:48

guide. This walks you through the seven

01:50

steps I take whenever I'm designing a

01:52

new system. So at the start of the

01:54

video, I already showed you the eager,

01:57

naive approach to this. And as you can

02:00

see, we now finally have the user

02:01

interface. So now I can do things with

02:04

the data like I can analyze the sales

02:06

data which as you can see is a huge

02:09

number of dollars or I can count the

02:12

total number of sales records as well

02:15

using that same data and of course now

02:17

the interaction is pretty fast because

02:19

we already loaded the data. Quitting is

02:22

also really fast which is great. Now of

02:24

course you don't want your user to wait

02:25

10 seconds just to get to the menu. It's

02:28

not great UX. So let's make this more

02:30

responsive by deferring that loading of

02:33

all the data until it's actually needed.

02:35

So if I go back to the main file, as you

02:38

can see, what happens here is that at

02:39

the start of the main function, I

02:41

actually load the sales data. So a very

02:44

simple way that you could do lazy

02:46

loading is by basically taking this

02:49

and moving it to the point where you

02:53

actually made a choice

02:56

like so. And now when I run the main

02:58

file again, I get the user interface

02:59

immediately. Now, of course, as soon as

03:01

I then pick one of these options like

03:04

analyzing the sales data, well, then

03:06

we're back to loading all of that data

03:08

again, which simply takes a lot of time.

03:11

And in fact, we made it kind of worse

03:13

because now if I pick the second option,

03:15

counting the total sales record, well,

03:18

it's going to load the CSV data again.

03:21

So it seems like we traded that initial

03:23

loading time that we had when starting

03:25

the application for something that's

03:27

arguably even worse because now the user

03:29

always has to reload all that data every

03:32

time and that's also not really what you

03:34

want, right? At least quitting is still

03:36

fast. That's something one thing you can

03:39

do to avoid having to load the data all

03:41

the time is to combine the lazy loading

03:43

pattern with caching. And that's

03:46

actually pretty easy to set up in Python

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

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

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

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

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

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

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