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
ابزارهای یادگیری هوش مصنوعی را باز کنید
ثبتنام کنید تا به ابزارهای قدرتمندی دسترسی پیدا کنید که به شما کمک میکنند سریعتر از هر ویدیو یاد بگیرید.
The Lazy Loading Pattern: How to Make Python Programs Feel Instant
زیرنویسها (476 بخشها)
I have a script here that loads data
from a pretty large CSV file. As you can
see, there's like millions of records in
here. Now, I'm doing this in a pretty
naive way. I'm simply loading it here.
And then I have some functions to
analyze sales, count the number of
sales, and there's a simple textbased
user interface.
This is what happens when I run this
code. So, as you can see, it loads all
that CSV data, and that actually takes a
pretty long time. So, the program is not
frozen. It's working, but it's loading
these millions of rows before doing
actually anything useful. This is a
classic example of eager loading. But
today, I'm going to fix this by using
the lazy loading pattern and show you a
couple of things that you can do.
Actually, the concept of lazy loading
dates back to already 1960s when
operating systems began loading memory
pages only when accessed. Martin Fowler
later popularized it in patterns of
enterprise application architecture,
describing how OMS like Django delay
database queries until the data is
actually used. And you probably also use
this daily yourselves by using uh
Django, SQL Alchemy, TensorFlow and even
Pandas rely on it to handle large data
more efficiently. And there's many other
real life examples of lazy loading as
well in uh websites that already show
the UI before the data is actually there
or operating systems that show you the
login screen while it's still in the
background loading all sorts of stuff.
So let's see how you can use that same
principle in Python code. Now before we
start, if you want to learn more about
how to design a piece of software from
scratch, I have a free guide for you at
iron.codes/design
guide. This walks you through the seven
steps I take whenever I'm designing a
new system. So at the start of the
video, I already showed you the eager,
naive approach to this. And as you can
see, we now finally have the user
interface. So now I can do things with
the data like I can analyze the sales
data which as you can see is a huge
number of dollars or I can count the
total number of sales records as well
using that same data and of course now
the interaction is pretty fast because
we already loaded the data. Quitting is
also really fast which is great. Now of
course you don't want your user to wait
10 seconds just to get to the menu. It's
not great UX. So let's make this more
responsive by deferring that loading of
all the data until it's actually needed.
So if I go back to the main file, as you
can see, what happens here is that at
the start of the main function, I
actually load the sales data. So a very
simple way that you could do lazy
loading is by basically taking this
and moving it to the point where you
actually made a choice
like so. And now when I run the main
file again, I get the user interface
immediately. Now, of course, as soon as
I then pick one of these options like
analyzing the sales data, well, then
we're back to loading all of that data
again, which simply takes a lot of time.
And in fact, we made it kind of worse
because now if I pick the second option,
counting the total sales record, well,
it's going to load the CSV data again.
So it seems like we traded that initial
loading time that we had when starting
the application for something that's
arguably even worse because now the user
always has to reload all that data every
time and that's also not really what you
want, right? At least quitting is still
fast. That's something one thing you can
do to avoid having to load the data all
the time is to combine the lazy loading
pattern with caching. And that's
actually pretty easy to set up in Python
زیرنویس کامل در پخشکننده ویدیو موجود است
با تمرینها یاد بگیرید
تمرینهای واژگان، گرامر و درک مطلب از این ویدیو بسازید
نظرات (0)
برای نظر دادن وارد شویدثبتنام کن و همه امکانات رو باز کن
پیشرفتت رو دنبال کن، واژگان رو ذخیره کن و تمرین کن
حالت تعاملی
آزمون
پاسخ صحیح:
ویدیوهای مرتبط
AI RECONSTRUCTS HITLER'S SPEECH TO ENGLISH (Extended)
This Design Pattern Scares Me To Death
Why Your Code Isn’t Pythonic (And How to Fix It)
Shadows In Compose | Jetpack Compose Tips
Now in Android: 118 –What’s new in Android development at Google I/O 2025 (part 2)
Late Night with Seth Meyers
آزمون
پاسخ صحیح:
آزمونها هنگام تماشای ویدیو ظاهر میشوند
راهنمای حفظ
از این ویدیو
شروع رایگان یادگیری زبان