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
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?
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?
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
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.
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.
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.
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.
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.
I would like see more topics about Multithreaded and AsyncIO. Thanks!
+
I give like even before the video starts. You're the best! Thank you.
dois! hehe
Thanks Miguel for sharing your knowledge! It means a lot! 👌
after 2 hours of pain I see you video and it works instantly thx you fella you carried this !
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
Good explanation what about same scenario but more than one threads. Everyone of em is going to print iteration complete statement before exiting?
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?
Great content. Do you know if signals are also supported on Windows?
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?
found this immensely useful. how can i do that for certain condition when using multithreads
Thank you, man.
Exactly what needed, good work....👍
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
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.
Appreciate the effort!
I will actually use this in a few hours!
man thank you so much this helped me a lot
you saved my day Sirq
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.
I say it in the video. You cannot kill a Python thread. You have to make it stop gracefully.
Can I do the same using multiprocessing?
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?
You just set the event, no difference.
I see, thank you.
Amazing, please keep making these videos ❤️
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.
eh I'm gonna risk adding a potentially infinite loop and squash the program if need be, wish me luck
But this exits the entire script, but i want to keep the script running and only end a specific thread.
Catch the KeyboardInterrupt exception, and just only set the exit event and exit the thread you want.
PLEASE MAKE PLAYLIST IN YOU CHANNEL
thanks
Thank yoiu !!!!!!!!!!
Wow,that's great
How to kill the thread performing long running DB queries
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.
aaaaa