No video

How Do You Kill a Python Thread?

  Рет қаралды 23,775

Miguel Grinberg

Miguel Grinberg

Күн бұрын

Two ways to kill a Python thread!
Support my work on Patreon: / miguelgrinberg

Пікірлер: 39
@oleksisfraga
@oleksisfraga 3 жыл бұрын
I would like see more topics about Multithreaded and AsyncIO. Thanks!
@bekbull
@bekbull 3 жыл бұрын
+
@rohitbhanot7809
@rohitbhanot7809 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.
@Felipe-53
@Felipe-53 3 жыл бұрын
I give like even before the video starts. You're the best! Thank you.
@RamonChk
@RamonChk 3 жыл бұрын
dois! hehe
@Globulov
@Globulov 2 жыл бұрын
after 2 hours of pain I see you video and it works instantly thx you fella you carried this !
@aminghanooni4240
@aminghanooni4240 3 жыл бұрын
Thanks Miguel for sharing your knowledge! It means a lot! 👌
@soneshdabhi3990
@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
@0xmkay
@0xmkay 6 ай бұрын
Good explanation what about same scenario but more than one threads. Everyone of em is going to print iteration complete statement before exiting?
@Sith90lord
@Sith90lord 6 ай бұрын
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?
@dipeshsamrawat7957
@dipeshsamrawat7957 10 күн бұрын
Thank you, man.
@user-rs8wp1mk4p
@user-rs8wp1mk4p 5 ай бұрын
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-maciel
@thales-maciel 3 жыл бұрын
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.
@yashgusain621
@yashgusain621 Жыл бұрын
found this immensely useful. how can i do that for certain condition when using multithreads
@haridevel5003
@haridevel5003 2 жыл бұрын
Exactly what needed, good work....👍
@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?
@miguelgrinberg
@miguelgrinberg Жыл бұрын
You just set the event, no difference.
@seyproductions
@seyproductions Жыл бұрын
I see, thank you.
@scottlee38
@scottlee38 3 жыл бұрын
Appreciate the effort! I will actually use this in a few hours!
@tekknokrat
@tekknokrat Жыл бұрын
Great content. Do you know if signals are also supported on Windows?
@hkgeek2408
@hkgeek2408 2 жыл бұрын
you saved my day Sirq
@unknownblueblue
@unknownblueblue 2 жыл бұрын
man thank you so much this helped me a lot
@vaibhavkumarchaudhary
@vaibhavkumarchaudhary 3 жыл бұрын
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
@grothaal
@grothaal 3 жыл бұрын
Can I do the same using multiprocessing?
@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.
@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.
@soumajitghosh02
@soumajitghosh02 3 жыл бұрын
thanks
@histavista8146
@histavista8146 3 жыл бұрын
PLEASE MAKE PLAYLIST IN YOU CHANNEL
@vaibhavnayak6521
@vaibhavnayak6521 Жыл бұрын
Thank yoiu !!!!!!!!!!
@dennisasamoah2213
@dennisasamoah2213 3 жыл бұрын
Wow,that's great
@sureshsubramaniyan4919
@sureshsubramaniyan4919 3 жыл бұрын
How to kill the thread performing long running DB queries
@miguelgrinberg
@miguelgrinberg 3 жыл бұрын
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 Жыл бұрын
aaaaa
threading vs multiprocessing in python
22:31
Dave's Space
Рет қаралды 577 М.
Python Tutorial 20: Understanding Python Threads and Threading
30:56
Paul McWhorter
Рет қаралды 18 М.
Секрет фокусника! #shorts
00:15
Роман Magic
Рет қаралды 63 МЛН
Just Give me my Money!
00:18
GL Show Russian
Рет қаралды 991 М.
Prank vs Prank #shorts
00:28
Mr DegrEE
Рет қаралды 13 МЛН
Мы сделали гигантские сухарики!  #большаяеда
00:44
Tkinter - Multithreading
38:10
JobinPy
Рет қаралды 6 М.
Python 3 - Episode 45 - Thread Pools
13:52
VoidRealms
Рет қаралды 7 М.
ASMR Programming - Spinning Cube - No Talking
20:45
Servet Gulnaroglu
Рет қаралды 3,9 МЛН
Four SQLAlchemy Tips
19:14
Miguel Grinberg
Рет қаралды 12 М.
Python multithreading 🧵
13:34
Bro Code
Рет қаралды 64 М.
Building A Security System In Python...🚨
17:56
Tech With Tim
Рет қаралды 50 М.
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 302 М.
STOP Learning These Programming Languages (for Beginners)
5:25
Andy Sterkowitz
Рет қаралды 688 М.
Modern Python logging
21:32
mCoding
Рет қаралды 182 М.
Error Handling in the Real World - Portland Python User Group
39:13
Miguel Grinberg
Рет қаралды 13 М.
Секрет фокусника! #shorts
00:15
Роман Magic
Рет қаралды 63 МЛН