WEBVTT 1 00:00:00.050 --> 00:00:01.227 Hi, welcome back. 2 00:00:01.227 --> 00:00:02.652 In this video, I just wanted to tell you 3 00:00:02.652 --> 00:00:04.728 a little bit about killing threads. 4 00:00:04.728 --> 00:00:07.210 I've gone back here in the code to the code before 5 00:00:07.210 --> 00:00:09.579 using the ThreadPoolExecutor, 6 00:00:09.579 --> 00:00:11.602 and notice that we've got thread1.start 7 00:00:11.602 --> 00:00:13.111 and thread2.start. 8 00:00:13.111 --> 00:00:15.634 Then we've got space to write more code, 9 00:00:15.634 --> 00:00:17.797 and then we wait for them to finish. 10 00:00:17.797 --> 00:00:21.002 Inside this space here, you could write some code 11 00:00:21.002 --> 00:00:25.127 to kill the thread, for example, if it meets some criteria 12 00:00:25.127 --> 00:00:28.070 or maybe you no longer need it or something. 13 00:00:28.070 --> 00:00:31.596 You could write some code to kill the thread, 14 00:00:31.596 --> 00:00:34.199 but you should not do that. 15 00:00:34.199 --> 00:00:38.530 Don't kill a thread. Wait for it to finish. 16 00:00:38.530 --> 00:00:43.440 If you kill a thread manually without being really careful, 17 00:00:43.440 --> 00:00:47.991 what can happen is that your thread has acquired the GIL, 18 00:00:47.991 --> 00:00:52.010 you kill it before it releases the GIL, 19 00:00:52.010 --> 00:00:55.466 and then what you've got is the GIL is gone. 20 00:00:55.466 --> 00:00:56.995 No other thread can pick it up 21 00:00:56.995 --> 00:00:58.713 because it wasn't released before. 22 00:00:58.713 --> 00:01:01.880 Threads will not release the resources 23 00:01:03.062 --> 00:01:04.710 just by killing them. 24 00:01:04.710 --> 00:01:06.527 You need to release the resources manually 25 00:01:06.527 --> 00:01:09.205 in the thread code itself. 26 00:01:09.205 --> 00:01:10.916 If you have your thread and it's started, 27 00:01:10.916 --> 00:01:12.782 and it's using the GIL and then you kill it, 28 00:01:12.782 --> 00:01:15.832 the GIL is gone, no other thread can get the GIL, 29 00:01:15.832 --> 00:01:18.016 your Python programme will stop. 30 00:01:18.016 --> 00:01:20.275 It won't terminate, because the thread here 31 00:01:20.275 --> 00:01:22.314 will be waiting to finish, 32 00:01:22.314 --> 00:01:24.579 but it won't be able to do anything, 33 00:01:24.579 --> 00:01:27.142 and if that happens, if you have a resource 34 00:01:27.142 --> 00:01:30.698 that's being used and nothing else can use it, 35 00:01:30.698 --> 00:01:32.924 this is called a deadlock in computing, 36 00:01:32.924 --> 00:01:35.209 and it's a bad thing. 37 00:01:35.209 --> 00:01:37.725 So, just be careful. Don't kill threads. 38 00:01:37.725 --> 00:01:39.531 And that's just all I say. 39 00:01:39.531 --> 00:01:41.683 Don't kill threads, it's not easy to do, 40 00:01:41.683 --> 00:01:43.212 and I don't recommend it. 41 00:01:43.212 --> 00:01:45.143 If you kill threads, you're going to have problems, 42 00:01:45.143 --> 00:01:46.662 so don't do it. 43 00:01:46.662 --> 00:01:48.767 That's it for this video. I'll see you in the next one.