Threading in Python - Advanced Python 16 - Programming Tutorial

Поділитися
Вставка
  • Опубліковано 21 жов 2024

КОМЕНТАРІ • 48

  • @kevinjones238
    @kevinjones238 3 роки тому +4

    Subscribing. Python Engineer, your tutorials are filled with well delivered content. Thank you for your time and effort😲!!!

  • @vijaybabaria3253
    @vijaybabaria3253 Рік тому

    Love your content, thank you.
    future topic suggestion : pytest integration test 🙏🏼

  • @sigmasix09
    @sigmasix09 2 роки тому

    Thanks for the video, nicely explained

  • @vladimirtchuiev2218
    @vladimirtchuiev2218 2 роки тому +2

    I hear "daemon thread" as "demon threat", I played too many videogames...
    To quote: "A demon threat is a background threat that will die when the main threat dies"

  • @squeakycamp207
    @squeakycamp207 3 роки тому +1

    Amazing video! Thank you so much for making this great explanation!

  • @kosmonautofficial296
    @kosmonautofficial296 2 роки тому

    Very good video thanks so much!

  • @sharifmansuri607
    @sharifmansuri607 4 роки тому

    Superb video on multithreading topic Explanation is absolutely crystal clear... Thanks for uploading video

  • @hackdesigner
    @hackdesigner 3 роки тому +1

    I don't get your example for `with lock: ` usage. With GIL enabled, how can one print() call be executed inside another one? ~Or, printing to Console is IO-bound operation?~
    EDIT: OK, read an article and understood: the main text and '
    ' can be interjected with more text because how print() itself works.

  • @alvfig
    @alvfig 2 роки тому +2

    I did think Queue was already thread safe, that it won't require a lock. Anyway, thank you by the great content.

  • @liushaomin7004
    @liushaomin7004 3 роки тому +3

    If we lock the thread, won’t that defeat the purpose of threading(that is to switch to another task while waiting for IO to respond?)
    Are there other ways to communicate, while ensuring that we can still switch between tasks?

    • @jasoncole3253
      @jasoncole3253 3 роки тому

      I was thinking about the same, the Lock option is completely useless as it just acts exactly like the GIL, which means that you can achieve the same results without having to write ad hoc threading code

    • @2LazySnake
      @2LazySnake 2 роки тому +2

      It seems like Lock really nullifies the purpose of threading in this particular example just because we have time.sleep(0.1) inside our lock code. Say we have a few hundred urls where we want to make PUT requests and make thousands of changes. Some of urls will have multiple requests and we wish to be sure that no racing is happening. So we could acquire a lock for each distinct while allowing other uls be processed in (almost) parallel.

    • @MouradMourad1000
      @MouradMourad1000 Рік тому

      generally speaking, multithreading is completely useless (or even worse) fir CPU bound tasks (opposed to IO bound tasks), because of the GIL.

  • @caiyu538
    @caiyu538 2 роки тому

    really nice. thumb it up.

  • @vi5hnupradeep
    @vi5hnupradeep 3 роки тому

    Thank you so much ! Great content 💯💯

  • @kosonko9322
    @kosonko9322 5 років тому

    Thank you guy, very helpfull and very good explanation ;D

  • @benyaminyakobi3652
    @benyaminyakobi3652 2 роки тому

    Thank you very much for detailed & clear explanation. Rare type of great content!

  • @erfelipe
    @erfelipe 3 роки тому

    Thanks!

  • @kaxurxsurt3987
    @kaxurxsurt3987 3 роки тому

    Hello Patrick, please explain this to me again:
    In your video at 21:51
    1. why does the main thread die if there is an infinite loop?, does q.join() somehow stops it ?
    I understand that after task_done() the thread should still be 'stuck' in the loop even though q.join() 'gathers' all the final results but...
    2. ...how does it stop the loop? and
    3. why would we use an infinite loop in the first place?
    I am sorry if my questions seem very obvious, thank you in advanced and thank you for this material.

    • @ChupoCro
      @ChupoCro 3 роки тому +5

      1: The infinite loops are not inside the main thread so the main thread ends like every normal program - after the last instruction (print in this case). q.join() stops the main thread until the queue becomes empty. Without join() the main thread might finish executing it's last instruction (print) before *all* the data in the queue has been processed by the multiple threads and those threads would have been destroyed too early (before processing all the data).
      2: The loops don't stop and there is not only one loop - there are multiple loops, each thread has it's own infinite loop. When the main program reaches the end then all the threads (together with their loops) are destroyed. The only problem is to assure the main program would not end before all the data has been processed in the separate threads and that's what join() is for - so the main thread waits until the queue becomes empty.
      3: The infinite loops (plural) in the threads are because it is not known in advance how many times will each thread process the data from the queue, when will it process the data and it is even not known *if* every thread will be processing the data. As you can see in the output - after one of the threads processes the first data from the queue it is not known which thread will process the next data. Without infinite loops in the threads there should be a way for each thread to know when all the processing is done and in that case the loop should end but much simpler is to just use infinite loops and to destroy the threads when the main program exits. Without daemon = True the threads would stay running in the memory even after the main thread would end. In that case some condition would have to be used to break from the loops so the threads wouldn't stay running in the memory even after the main program ends.

    • @kaxurxsurt3987
      @kaxurxsurt3987 3 роки тому

      @@ChupoCro got it, thank you very much!

    • @patloeber
      @patloeber  3 роки тому

      nice!

    • @tomaszwojcik751
      @tomaszwojcik751 3 роки тому

      ​@@ChupoCro​ @Python Engineer @Kaxurx surt
      3a): As I am understanding and I tried to run worker without infinite loop "while True", what loop does, it keeps aliveness of the Theard, preventing the Thread from ending after single invocation. Do I am right?
      3b) Is there any other way to bind Threads with something else than Queue or is this unseparable?
      3c): For me (using PyCHarm + Python 3.7.3) does not ended program itself with returncode 0 (It seems thread.deamon=True) does not work. It does ended program itself (has print "end main"), but was working without ending, I had to kill process. What helped, was instruction: if q.empty(): break at the end of infinite while loop.
      3c): OK, thread.deamon did not worked, because i should write thread.daemon...
      3c): is there any way to kill thread? For instance:
      for thread in threads:
      if thread.is_alive():
      thread.operation_to_kill()
      ?
      Ps. Kind thanks for explanation.

  • @valigeorge9110
    @valigeorge9110 2 роки тому

    thanks!

  • @abhisekhagarwala9501
    @abhisekhagarwala9501 4 роки тому +1

    hi , i have a doubt i would appreciate it if you could ans.
    if i have a flask service in which a function is there which gets executed
    app.route(/, ['POST'])
    def sum():
    addition , status = addition(x,y)
    q1 - if some one wants send multiple request to my api how do i make it concurrent as well as thread safe,???
    q2 -> and if different threads are getting created then should i have to make variable inside the addition method thread safe or they just local variable and won't be affected.
    q3-> add function return two values will they be affected by different threads. and how do i get return values after writing threading code.
    should i do like
    addition, status = thread.start()
    ????

  • @codeone6423
    @codeone6423 Рік тому

    thanks

  • @Loug522
    @Loug522 2 роки тому

    For some reason the final code doesn't work for me in vscode and doesn't even return an error, while on pycharm it works just fine.
    EDIT: it started working but only if I use the debug mode

  • @intisarmuhtadee1117
    @intisarmuhtadee1117 4 роки тому

    Which IDE / Text editor you are using?

  • @darrenlefcoe
    @darrenlefcoe Рік тому

    i have never encountered a race condition, but if i do, the `Lock` will be ideal...

  • @alanjohnstone8766
    @alanjohnstone8766 2 роки тому

    I have listened to all of this series of videos and found them exceptionally well explained - up until this one. Q.join came from nowhere and was not explained and the whole rather complicated interaction of the various parts was unclear. Sorry but it is better to be honest. Alan

  • @PRATIK24ADARSH
    @PRATIK24ADARSH 3 роки тому

    is anyone else hearing 'thread' as 'threat' ?

  • @spy6924
    @spy6924 4 роки тому +1

    Everyone is İndian in here ı think so...(but ı am not ındıan)

    • @patloeber
      @patloeber  4 роки тому +1

      India is #1 with 23% according to my UA-cam Analytics ;)

    • @spy6924
      @spy6924 4 роки тому

      @@patloeber for me too

    • @luiswiederhold752
      @luiswiederhold752 3 роки тому

      @@patloeber you Sound german tho

    • @patloeber
      @patloeber  3 роки тому

      @@luiswiederhold752 I am German ;)

    • @luiswiederhold752
      @luiswiederhold752 3 роки тому

      @@patloeber Wie nice haha, bin 15 und komme auch aus Deutschland:)