RUVIDEO
Поделитесь видео 🙏

2. Threading in Python

lets us basically say we'll get back to this later. So right now, I won't do anything.

And we're going to have the syntax here if underscore, underscore, name, underscore, underscore is equal to.

And then we've got underscore. Understood. Oh, underscore. Underscore, main. Underscore, underscore.

Then we're going to run the main function. Now, basically, what this syntax means is that if this program is directly run, not when it's imported or anything, just when this program is specifically run, that's when this will get executed. Otherwise, if we import this, then it won't get executed.

So this is just quite a nice syntax. I quite like it. And you'll likely see this in a lot of python programs. Of course, it's not completely necessary, but it's kind of nice if you want to test the code that you're writing here itself when you kind of want to run it. But otherwise, if you want to import it, you don't want it to be run.

All right. So we have our main function here. And basically what we're going to be doing is we're going to be writing well, two functions.

The first function is going to calculate the sum of squares. So we're going to call it off, calculate some squares, and it's going to have an input. We can just say and for now, we'll find out in a second.

And our second function is just going to sleep a little. And then we're just going to provide in here the input in seconds for how long we would like it to sleep.

So we're going to have our calculation start time was going to be defined as the current timestamp at this moment.

And then once this is done, we're going to say, um, calculating sum of squares took.

And then we're going to take that time at the current moment, minus the timestamp that we have at this

moment.

When we first, you know, saved our variable here.

Now, this is going to give us an update with a.

Lot of decimal numbers.

So what we're going to do instead is we're going to use the round method and we're just going to round

it to one decimal place place to get this kind of reduced a little bit, a little bit cleaner.

And then what we're going to do for sleep little, we're going to do a similar thing.

We're going to have a sleep start time, which is also just going to be the tracking the time at this moment, saving them into a variable.

And then again, here, we're going to be sleeping took.

The current time, minus the start time, that's how long it took, and to actually have the calculations take some time, we're not just going to put in your eye, but let's go ahead and make this be I plus one. First of all. And then we're also going to multiply it by one million just so that it takes a little bit of time.

Now, we can run this, um, just for example, clicking the run here, and it's going to well, start running this sequential program for us.

And so we can see here currently the sum of squares is that work took about seven seconds rounded to one decimal places we can see here. And now our program is just going to be sleeping interactively.

So one second, two seconds, three for a second, five seconds. And then at the end of that, we're going to see, OK. So that whole process took 15 seconds.

So in total, our program took about twenty one seconds or twenty two seconds. It's got fifteen here, plus seven. So seven of those were for the calculation and fifteen of those were for the sleeping. All right. So this is a sequential program.

So let's go ahead and try to add some concurrency here. We're going to use threating for this. So we're going to import threading. Now, the nice thing is threading is actually part of the standard pattern installation. So you don't really have to install anything else. You can just import directly.
Now, for each of these loops here, we're now going to replace this instead with a thread. So we're going to do in each of these cases is we're going to create a thread and we'll call it T. It's just going to be your short variable for thread threatening dot thread.

And now there are several variables that we need to define here. The first thing is the target, which is going to be the function that needs to be run. And for that for this case, it's going to be calculate sum of squares. Note, I am not putting the parentheses at the end. It's just the function name, because we don't want to call it yet. This is kind of the reference to the function.

Then we can also provide the arguments to our function. We have a separate keyword here, ARGs, and this expects a tuple. So since we only have one input argument, we're going to have our input value here and then we're going to roll. Let's have our let's define our.
Maximum value is going to be this year just to have it, you know, be a little bit shorter and sort of input Tupelo's going to be this. But since it has to be a tuple, we have to put an extra comma here to make sure that it's a tuple of size one. And we're going to comment this out and then we're going to do the same thing here.

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «2. Threading in Python», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.