Python Threads - MultiThreading in Python and Python GIL - Python MultiProcessing

Поделиться
HTML-код
  • Опубликовано: 5 авг 2024
  • Welcome to the video series multithreading and multiprocessing in python programming language and in this video we'll also talk about the famous or rather infamous python Global Interpreter Lock or Python GIL.
    This video starts with clarifying that fact that python threads are indeed real threads and there is one kernel level thread is created for every python thread we create from python programming language.
    After understanding the same, we'll learn in detail about the python GIL or Python Global Interpreter Lock and we'll also see how it works by creating multiple threads and understand how python GIL impacts the execution of multiple threads. I've also created a comparison between C++ threads and Python threads so that we understand the same in the easiest possible way.
    I'll create multiple threads by creating CPU intensive jobs so that we can understand how it impacts the multithreading in python. I've also touched upon the multiprocessing in python and explain the scenarios on which you need to use python multiprocessing and not the multithreading.
    At the end of this video, I've explained the Object Oriented way of creating the python threads and then how we can create a class by deriving the same from python Thread. In general, the OOP way is more acceptable way to create the thread.
    Hope this video will help you in expediting your learning journey. Here are the contents of this video
    Timecodes :
    0:00 - Multithreading in Python
    0:40 - CodeSports.ai intro
    0:58 - Multithreading topics of this video
    1:47 - Python Thread class and threading
    2:12 - Creating Python threads using builtin python Thread class
    2:50 - Python Threads are Real Threads
    4:21 - Creating the Threads in Python
    4:48 - Python threads needs to be started explicitly
    5:10 - Seeing Python Threads in the system using linux top command
    6:03 - Python Thread join - wait for the thread to complete
    6:41 - Python GIL - Python Global Interpreter Lock
    8:58 - Creating Multiple Threads in Python
    9:35 - How Python GIL impacts the execution of Threads
    10:40 - Comparing python threads with C++ threads
    13:48 - MultiProcessing in Python for CPU intensive Jobs
    15:33 - Python Threads creating in Object Oriented Way
    #python #multithreading #multiprocessing #cpu #cores #memory #software #softwaredevelopment #programming #pythonprogramming
    About Me i.e. An Insightful Techie
    Greetings and Thanks a lot for checking out my RUclips Channel. I’m Deepak K Gupta (Daksh). This channel is a medium for me to share my learnings and journey as a Techie so that you not only learn from my experience but also from my mistakes
    As a techie I am closely associated with software development, so most of my content will revolve around Programming Languages, AI, Machine learning and Databases. I share things which will help you learn, remember and use these things in an effective and efficient way.
    As a Techie, I also know that there is a life beyond software development and it has a profound impact on our career as well as on our well being. I also share those learnings which I feel are worth sharing with the intention that it MAY help you in taking better decisions in your life.
    Last but not the least, I'm a traveler and explorer by nature. I would like to take you with me on some of the interesting journeys around the world. Believe me there is more to learn outdoors than indoors
    Hope you’ll like my contents and will be part of my journey 🙏
    SUBSCRIBE - ruclips.net/user/AnInsightfulT...
    INSTAGRAM - www.intagram.com/AnInsightful...
    Twitter - / awisetechie
    Facebook - / aninsightfultechie
  • НаукаНаука

Комментарии • 78

  • @dimberabha2620
    @dimberabha2620 3 года назад +9

    This is what a developer wants. The difference you have shown is perfectly helpful. Thank you sir

  • @akashjain35
    @akashjain35 2 года назад +2

    Now this is what I call quality content. Subbed!

  • @raghvendramishra2862
    @raghvendramishra2862 Год назад

    Great !!!! I have gone through numerous texts and papers, and websites to clearly dig all these questions. But your one video gives answers for all of them. Thanks a lot Sir.

  • @Vijay-Yarramsetty
    @Vijay-Yarramsetty Год назад

    by far the most lucid explanation of threading vs multi processing in Python. Thanks very much.

  • @Mina.f.a
    @Mina.f.a Год назад +2

    Amazing teaching skills ! Thank you for teaching me this very effectively two minutes before my interview !!!

    • @Cognitive-Programmer
      @Cognitive-Programmer  Год назад

      Thanks for your comment. Glad that it was of some help. All the best for your interview 🙏

  • @sanjaybhatikar
    @sanjaybhatikar 2 года назад +1

    Awesome, thank you!

  • @aashishgoyal1436
    @aashishgoyal1436 3 года назад +1

    Amazing content sir. Please complete the series. It is really helpful

    • @Cognitive-Programmer
      @Cognitive-Programmer  3 года назад +1

      Thanks for your comment 🙏🙏🙏 sure I am planning to have more videos on this topic.. Thanks again

  • @anaryz_supa2653
    @anaryz_supa2653 2 года назад +1

    Thanks! Helped a lot.

  • @kosgeivictor6146
    @kosgeivictor6146 Год назад

    Perfect explanation, thank you.

  • @pakado2009sp
    @pakado2009sp 2 года назад +1

    GREAT VIDEO!!!!!! WELL DONE!

  • @mohitrathore8808
    @mohitrathore8808 Год назад

    Bestest Tutorial Ever

  • @sunilkoiri1030
    @sunilkoiri1030 2 года назад +1

    Awesome demo + explanation

  • @Parthiban_Straight_Edge
    @Parthiban_Straight_Edge 2 года назад +1

    Great explanation Daksh!! Keep it up

  • @popojamesforreal5667
    @popojamesforreal5667 2 года назад +1

    Nice video. Pretty helpful :)

  • @sinahariri6623
    @sinahariri6623 Год назад +1

    You explained it very clear thank you

  • @Vijay-Yarramsetty
    @Vijay-Yarramsetty Год назад

    this is great content. I wish I found it long back.

  • @temperkan3727
    @temperkan3727 8 месяцев назад +1

    Great video

  • @ananyamahapatra6597
    @ananyamahapatra6597 3 месяца назад

    Thank u so much Sir🙂

  • @ajiteshsingh3764
    @ajiteshsingh3764 2 года назад +1

    Perfect explanation

  • @aleksandarpetrovic2632
    @aleksandarpetrovic2632 3 года назад +1

    Amazing explanation, tnx

  • @PonnusamyK
    @PonnusamyK 2 года назад +1

    Thank you sir, Very usefull

  • @omkarmore2198
    @omkarmore2198 2 года назад +1

    Excellent

  • @tejasarlimatti8420
    @tejasarlimatti8420 2 года назад +1

    amazing video

  • @DevendraSingh-xf6cn
    @DevendraSingh-xf6cn 2 года назад +1

    Great Content! Just a question, when you started 2 threads. They both remained in active state. I can understand as 1 thread is running on while loop (infinite) but the other should close after a certain sleep time which is not happening in your case. Please explain: M i missing something here?

    • @Cognitive-Programmer
      @Cognitive-Programmer  2 года назад

      Thanks for your comment. Sorry I might not have understood your question properly, but if you're talking about 2 threads I create (in ~9th minute of this video), then both threads are created using same function i.e. working_thread
      So the behaviour is expected. Hope it clarifies your doubt

  • @468hitman
    @468hitman Год назад +1

    Very nicely explained

  • @vigneshkumar6451
    @vigneshkumar6451 2 года назад +1

    Wonderful explanation sir. 🙂🙂
    One small suggestion to run and show code using any IDE. Difficult to grab in jupyter notebook..
    Expecting ML series from you.
    Thanks..

  • @shushens
    @shushens 2 года назад +1

    Really like this. I hope you keep making more videos like this one.
    Kintu mama, threading video gulo diye ekta playlist baniye dile khub bhalo hoto.

    • @Cognitive-Programmer
      @Cognitive-Programmer  2 года назад

      Thanks for your comment 🙏
      Here is the playlist on Python Multithreading

  • @shubhashishsatpathy8736
    @shubhashishsatpathy8736 Год назад

    Great tutorial, really helpful. One question - at 8:26 not sure how setting kill_thread to True is picked up by the thread. If i were not using threading at all and had that while loop running infinitely, setting kill_thread to True later would not have stopped that loop. Why does it stop it for a thread?

    • @Cognitive-Programmer
      @Cognitive-Programmer  Год назад

      Hello
      Thanks for your comment .
      The thread function is continuously checking kill_thread variable in every iteration, so whenever it will be true, it will be stop the thread
      Hope it helps
      Thanks

  • @chebalid6797
    @chebalid6797 Год назад

    How would I determine the maximum numbers of threads a proccess can have in regards to the computer resources(RAm, cores etc)

    • @Cognitive-Programmer
      @Cognitive-Programmer  Год назад

      It's a system limitation and not of the programming language.. You need to check your system parameters.

    • @chebalid6797
      @chebalid6797 Год назад

      @@Cognitive-Programmer
      Example
      I have a linux machine with following
      Architecture x86_64
      CPU(s) : 4
      Threads per core : 2
      Core(s) per socket : 2
      Sockets(s) :1
      I have for example a python code, does it mean the most number of threads I can open on this specific code is 2(taking into account the main thread). Am confused about this yet I really want to have a firm understanding

    • @SodalisUK
      @SodalisUK 10 месяцев назад

      You can have as many threads as you want, but only 1 that can actually be executing python instructions at any instant because of the GIL.

  • @tastaslim
    @tastaslim Год назад

    An Insightful Techie I have this doubt, are threads closed automatically by python? I mean what if I don't use join() function, will thread be closed and if yes how does it happen and is there any certain amount of time for that. If you answer it, It will help me a lot. Thank you

    • @Cognitive-Programmer
      @Cognitive-Programmer  Год назад

      Hello
      Thanks for your comment. A thread will close automatically when it completes. You don't need to explicitly wait for join, but its a good practice to make sure that all your threads are closed before your program / function terminates.
      Hope it helps.
      Thanks

  • @haneulkim4902
    @haneulkim4902 Год назад

    Thanks for great video. I have few questions:
    1. Each thread takes up 100% CPU, my understanding is CPU_i refers to number of cores(i) then when 2 threads take up 100% of CPU_01, CPU_02 it means that in two cores it has one thread each which utilize all of its computation ability?
    2. My goal is distribute training deep learning model using tensorflow. I need to check if it is multi-threading/multi-processing before moving onto multi-device distributed training, I am very new to linux systems therefore do not know how to check if tensorflow utilizes all CPU and does multi-threading/processing. Could you recommend me some courses or materials that will help me understand how to analyze CPU usage?

    • @Cognitive-Programmer
      @Cognitive-Programmer  Год назад

      Hi
      Thanks for your comment.
      irrespective of the number of threads, only 1 CPU will be used. However, in case of Multi Processing, multiple CPU's can be used
      Hope it helps
      Thanks

    • @haneulkim4902
      @haneulkim4902 Год назад

      @@Cognitive-Programmer Thanks for the quick reply! I've edited my question, do you mind clarifying my understanding please?

  • @yt-1161
    @yt-1161 9 месяцев назад

    what is there wasn't GIL. would multi-threading and multi-processing be the same

    • @Cognitive-Programmer
      @Cognitive-Programmer  8 месяцев назад

      In terms of doing CPU intensive task within a process - Loseley Yes, but from an operating system perspective a thread and process are different. for example, you don't need IPC to communicate between threads.
      Hope it helps. Thanks 🙏

  • @Aditya-ne4lk
    @Aditya-ne4lk 2 года назад +1

    maybe you should have explained what is the purpose of GIL, i understood what it does, but not why

    • @Cognitive-Programmer
      @Cognitive-Programmer  2 года назад

      Thanks for your comment and suggestion.
      Without GIL, python will have to change completely as the current implementation will not run with GIL
      Hope it clarifies . Thanks again for your comment 🙏

    • @SodalisUK
      @SodalisUK 10 месяцев назад

      The purpose of the GIL is to avoid the need for locking objects every time you want to use or change them. If thread 1 is doing x += 1 and thread 2 is doing x -= 1 then x should stay the same but if they both get x at the same time the result will be wrong.
      Python also keeps a count of the number of references to an object this way, and if this incorrectly drops to zero, and the object is garbage collected into free memory and the memory reused, all sorts of chaos would happen.

    • @SodalisUK
      @SodalisUK 10 месяцев назад

      ​@@Cognitive-ProgrammerThere is a genuine proposal for a version without the GIL based on a successful proof of concept. But this will still be years away.

  • @NS-te8jx
    @NS-te8jx 2 года назад +1

    you should keep your code on git and share links here, so that we could do quick experiments on our machines while watching the videos.. that would make it more attractive and practical

    • @Cognitive-Programmer
      @Cognitive-Programmer  2 года назад

      Thank you for your valuable suggestion.. will implement for later videos

  • @hiteshpandey3824
    @hiteshpandey3824 2 года назад +1

    Should have found you sooner.

  • @vickkyphogat
    @vickkyphogat 2 года назад

    hindi Please!!!!!!!!!!!!!!!!!!