TU SIGUIENTE PASO

Python Thread Tutorial For Beginners

ProgrammingKnowledge · 15 lecciones · Gratis en YouTube

LECCIÓN 2 / 15

Python Thread Tutorial For Beginners 2 - thread module in Python 3

←Siguiente

Sobre este curso

hey guys welcome to the first video on Python thread tutorial for beginners in this video I'm going to give you a brief introduction about threads so first of

all what is a thread so a thread is an individual and separate unit of execution that is the part of a process now what is a process a process is simply our running program so for

example if you run a browser it's a process if you run a video game it's a process now inside this process multiple threads can work together to accomplish a common goal and threads allows the

program to run in parallel that is the single most important benefit of using threads so let me give you an example so when you play a video game it has to handle few tasks for example it has to

handle the graphics and the user interface and if you are playing the multiplayer game it has to handle the networking and all those things it has to handle in parallel because it has to

remain responsive at all the times so how does this videogame handles those parallel tasks a simple answer is threads it can assign one thread for graphics one thread for user interface

and other thread for networking so by using threads the beauty game can always remain responsive and it can run the task in a parallel way now let me explain threads using this diagram so

you can see this whole blue box here is a process and the threads exists inside this process so in this process you can see three threads now inside those threads you can see there is a code and

local variable so a thread is a light-weight process that exists inside the process also a thread is a sequence of control flow the difference between the thread and the process is

that the thread exists entirely inside the process and share its resources so let's say inside this gray box we have the global variables that each thread will be able to share those global

variables and in addition each thread can have its own local variables and its own control flow to work with those local and global variables now a single process may have multiple threads of

execution and as I already said that threads are useful when an application wants to perform many concurrent tasks on a shared data now let me give you one more example so think about a browser it

has to handle the landing pages or the animations or if you want to open multiple website on different tabs and you want to run video in one tab and if you want to do another task in other tab

most of the time the browser always remains responsive and the browser can handle multiple tasks at the same time so it uses threads to always remain responsive now there are two different

kinds of threads one is a kernel thread and other is a user space thread or a user thread now kernel threads are the part of operating system while userspace threads are not implemented in kernel

the userspace threads can be seen as an extension of function concepts in programming languages now each process have at least one and that is the main thread so if you

don't even create a thread inside a process it has at least one thread and that is process itself now let's talk about the advantages of using threading so the first advantage is multi-threaded

programs can run faster on computer systems with multiple CPUs because these threads can be truly concurrent on multiple CPU computer systems the second advantage is the program can remain

responsive to input at all the times this is true both for single and multiple CPU computer systems now the third advantage is threading allows to do something else while a thread is

waiting for IO tasks or any other tasks to complete so it always remains responsive the next advantage is some programs are easy to express using concurrency which leads to elegant

solution and as a result they are easier to maintain and debug so some programs or some processes are inherently concurrent in nature and the next advantage is threads of process can

share the memory of global variable so as I have already shown you in the diagram previously that multiple threads can share the memory of the global variables at the same time so if a

global variable is changed in one thread this change is valid for all the threads a thread can have local variables also as we have seen in the diagram now let's talk about some of the issues or

challenges you will face while programming threads for a program the first challenge is the scheduling so to execute a threaded program it must rapidly switch between threads so a

program should be able to switch between threads of gently and conveniently the second issue or challenge is the resource sharing so as I told you that multiple threads can

share the same memory of the global variables so since threads share this memory and other resources it must be careful because operation performed in one thread could cause problem in

another thread the next challenge you could face is of synchronizing of the threads so threads often need to coordinate their actions and as a software developer you may have heard

about race condition of threads which means the outcome depends on the order of thread execution and often for synchronization we use locking primitives now that was the general

description about threads now let's talk about the threads in Python so in Python a thread is an object like any other object that can hold data this object can also be used to run with methods the

object also can be stored in the data structures and this object can also be passed as a parameters to the methods also in Python our thread can also be executed as a process and during the

lifetime of a thread it can have various states now in Python there are two modules which supports the usage of threads and these two modules are thread module and treading module now there is

one thing to note here is that the tread module has been considered as deprecated for quite a long time so often users are encouraged to use that threading module instead of the thread module so if you

will see in Python 3 that thread module is directly not available but indirectly you can use underscore thread to use the thread module and this is to remain backward

with the previous versions of the Python now the difference between the thread module and the threading module is that the thread module treats the thread as the function while the module threading

is implemented in an object-oriented way so in the next videos I will show you how to use these two modules in Python to create threads and handle threads in Python so that's it for this video I

will see you in the next video you

SIEMPRE HAY ALGO MÁS

Sigue alimentando tu curiosidad.

Explorar más cursos
De la comunidad, para la comunidad

¿Encontraste un gran curso?

Compártelo. Nuestro equipo lo revisará antes de añadirlo al catálogo.