Two ways to kill a Python thread! Support my work on Patreon: / miguelgrinberg
Пікірлер: 39
@oleksisfraga3 жыл бұрын
I would like see more topics about Multithreaded and AsyncIO. Thanks!
@bekbull3 жыл бұрын
+
@rohitbhanot78093 жыл бұрын
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.
@Felipe-533 жыл бұрын
I give like even before the video starts. You're the best! Thank you.
@RamonChk3 жыл бұрын
dois! hehe
@Globulov2 жыл бұрын
after 2 hours of pain I see you video and it works instantly thx you fella you carried this !
@aminghanooni42403 жыл бұрын
Thanks Miguel for sharing your knowledge! It means a lot! 👌
@soneshdabhi3990 Жыл бұрын
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
@0xmkay6 ай бұрын
Good explanation what about same scenario but more than one threads. Everyone of em is going to print iteration complete statement before exiting?
@Sith90lord6 ай бұрын
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?
@dipeshsamrawat795710 күн бұрын
Thank you, man.
@user-rs8wp1mk4p5 ай бұрын
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?
@thales-maciel3 жыл бұрын
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
@miguelgrinberg3 жыл бұрын
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.
@yashgusain621 Жыл бұрын
found this immensely useful. how can i do that for certain condition when using multithreads
@haridevel50032 жыл бұрын
Exactly what needed, good work....👍
@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?
@miguelgrinberg Жыл бұрын
You just set the event, no difference.
@seyproductions Жыл бұрын
I see, thank you.
@scottlee383 жыл бұрын
Appreciate the effort! I will actually use this in a few hours!
@tekknokrat Жыл бұрын
Great content. Do you know if signals are also supported on Windows?
@hkgeek24082 жыл бұрын
you saved my day Sirq
@unknownblueblue2 жыл бұрын
man thank you so much this helped me a lot
@vaibhavkumarchaudhary3 жыл бұрын
Amazing, please keep making these videos ❤️
@michaeletzkorn3 жыл бұрын
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.
@michaeletzkorn3 жыл бұрын
eh I'm gonna risk adding a potentially infinite loop and squash the program if need be, wish me luck
@grothaal3 жыл бұрын
Can I do the same using multiprocessing?
@holycow48893 жыл бұрын
But this exits the entire script, but i want to keep the script running and only end a specific thread.
@rohitbhanot78093 жыл бұрын
Catch the KeyboardInterrupt exception, and just only set the exit event and exit the thread you want.
@danielrabolon30342 жыл бұрын
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.
@miguelgrinberg2 жыл бұрын
I say it in the video. You cannot kill a Python thread. You have to make it stop gracefully.
@soumajitghosh023 жыл бұрын
thanks
@histavista81463 жыл бұрын
PLEASE MAKE PLAYLIST IN YOU CHANNEL
@vaibhavnayak6521 Жыл бұрын
Thank yoiu !!!!!!!!!!
@dennisasamoah22133 жыл бұрын
Wow,that's great
@sureshsubramaniyan49193 жыл бұрын
How to kill the thread performing long running DB queries
@miguelgrinberg3 жыл бұрын
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.