How Do You Kill a Python Thread?

Поделиться
HTML-код
  • Опубликовано: 14 окт 2024
  • Two ways to kill a Python thread!
    Support my work on Patreon: / miguelgrinberg

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

  • @rohitbhanot7809
    @rohitbhanot7809 3 года назад +3

    This is really nice trick. Never heard of events from Threading library but yeah this makes handling threads at app exit much simpler and cleaner.

  • @oleksisfraga
    @oleksisfraga 4 года назад +13

    I would like see more topics about Multithreaded and AsyncIO. Thanks!

  • @Felipe-53
    @Felipe-53 4 года назад +3

    I give like even before the video starts. You're the best! Thank you.

  • @aminghanooni4240
    @aminghanooni4240 4 года назад +3

    Thanks Miguel for sharing your knowledge! It means a lot! 👌

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

    after 2 hours of pain I see you video and it works instantly thx you fella you carried this !

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

    Great video.
    I have a python webapp where users send a request which needs to do some background work, I launch a new thread for each request and return a request id to user immediately. Thread does it's task for about 5 minutes, and adds result to db.
    In some conditions I receive a stop request with request id for a previous request, how should I stop the thread I had started for that request id ? Is Is it a good idea to maintain a dict of request_id|thread on request and later use dict to stop that particular thread ? I don’t have any for loops in thread

  • @0xmkay
    @0xmkay 7 месяцев назад

    Good explanation what about same scenario but more than one threads. Everyone of em is going to print iteration complete statement before exiting?

  • @RicoDodzLugod
    @RicoDodzLugod 7 месяцев назад

    Nice tutorial Sir. I have question. Is it possible to stop only the specific thread and not to stop the main thread? Eg. i have main thread running, and have 4 threads running on that function. I want to stop the 3rd thread alone without affecting the rest of the threads. Is possible?

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

    Great content. Do you know if signals are also supported on Windows?

  • @Sith90lord
    @Sith90lord 7 месяцев назад

    What if i have no access to the code executed on the thread?
    In my case, I am given a piece of code to run on my thread, there is no while loop where i can check periodically for stop flags, it's just one function reference call.
    How would i tell the thread to stop executing then?
    Basically you can't touch your "bg_thread()" method.
    Are there no outside controls available to stop the thread execution?

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

    found this immensely useful. how can i do that for certain condition when using multithreads

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 Месяц назад

    Thank you, man.

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

    Exactly what needed, good work....👍

  • @thales-maciel
    @thales-maciel 3 года назад +1

    what if the function runs another unknown function that has a loop in It? I've been trying to figure It out how to handle this without knowing what the inner function looks like, but still no luck. My best bet so far is to use multiprocess, but that seems overkill

    • @miguelgrinberg
      @miguelgrinberg  3 года назад

      You cannot kill a thread, you can only signal it to exit voluntarily. If the loop in your function is not able to receive a signal to exit then there is no way in Python to stop that loop.

  • @scottlee38
    @scottlee38 3 года назад

    Appreciate the effort!
    I will actually use this in a few hours!

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

    man thank you so much this helped me a lot

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

    you saved my day Sirq

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

    Interesting and nice video, congratulations !! Do you know how to kill a thread (useless) from the program in order to free the resources ?
    I have not found information for that.

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

      I say it in the video. You cannot kill a Python thread. You have to make it stop gracefully.

  • @grothaal
    @grothaal 3 года назад

    Can I do the same using multiprocessing?

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

    What if I have a GUI where I want to have a Stop button to stop the thread? What do I write in the button's function to send the signal?

  • @vaibhavkumarchaudhary
    @vaibhavkumarchaudhary 4 года назад

    Amazing, please keep making these videos ❤️

  • @michaeletzkorn
    @michaeletzkorn 3 года назад

    But what happens if I force close python while threads are running? What if a thread gets stuck in an infinite loop hypothetically? I have threads marked as daemon but I can't keyboard interrupt them.

    • @michaeletzkorn
      @michaeletzkorn 3 года назад

      eh I'm gonna risk adding a potentially infinite loop and squash the program if need be, wish me luck

  • @holycow4889
    @holycow4889 3 года назад

    But this exits the entire script, but i want to keep the script running and only end a specific thread.

    • @rohitbhanot7809
      @rohitbhanot7809 3 года назад

      Catch the KeyboardInterrupt exception, and just only set the exit event and exit the thread you want.

  • @histavista8146
    @histavista8146 4 года назад

    PLEASE MAKE PLAYLIST IN YOU CHANNEL

  • @soumajitghosh02
    @soumajitghosh02 3 года назад

    thanks

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

    Thank yoiu !!!!!!!!!!

  • @dennisasamoah2213
    @dennisasamoah2213 4 года назад

    Wow,that's great

  • @sureshsubramaniyan4919
    @sureshsubramaniyan4919 4 года назад

    How to kill the thread performing long running DB queries

    • @miguelgrinberg
      @miguelgrinberg  4 года назад

      Well, first of all, you should optimize your database so that you don't have long queries. The daemon thread trick will work if the thread is running a db query. But as I say in the video, there is no opportunity to do cleanup, your database will probably continue running the query until it completes, even though the application exited. If you use the Event solution, then you can check for the state of the event object in between queries. If you interrupt the application the currently running query will complete, and then the thread will have a chance to exit cleanly.

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

    aaaaa