How do I have to stop a running thread in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How do I have to stop a running thread in Python?

I want to stop a running thread in Python I have prepared an example here: https://code.sololearn.com/cYBUxF0Qc7S6/?ref=app Despite I have read the relevant articles on stackoverflow, Python.org, tutorialspoint, geeksforgeeks and others I HAVE NOT THE SLIGHTEST CLUE how to stop the thread 't' i.e. the function 'count()' in the designated place of the code. Could one please complete the code in a procedural way. The examples which I have seen are all object orientated and i do not come clear with these.

11th Jan 2020, 7:18 AM
Jan Markus
5 Answers
+ 7
Sarthak 🇳🇵 I want to stop the execution of the function 'count()' after the execution of the function 'characters()' has ended. In the attached examples I want to let run the functions 'count()' AND 'characters()' at the same time first. When the function 'characters()' has finished for the first time, the function 'characters()' has to run a second time, but now alone without the function 'count()'.
11th Jan 2020, 7:59 AM
Jan Markus
+ 4
Sarthak 🇳🇵 Thank you for your suggestion. I am not experienced in the threading- / multiprocessing-topic. As far as I understood the material what I sighted from a layman's point of view is that multiprocessing is used to run independent processes, and these are isolated from each other so that it is very difficult to exchange data between the processes. Further on I had a look on the Threading-module, but it seems that there is no easy way to stop a running thread. You can start a thread easyly by t = threading.Thread(target=function) t.start() but I am a little disappointed, that there is not a method t.stop() in the threading module. In my slightly adapted code https://code.sololearn.com/camqCK4jHCPc/?ref=app I have a solution which works for me at the moment. I solved the problem with a flag tf. If it is set to True, the function runs, if it is False, the relevant part in the function is bypassed. In the latter case there remains an orphaned thread.
12th Jan 2020, 6:53 AM
Jan Markus
+ 2
"""Made a code to terminate it. Instead of using Threading.Thread, module, I used multiprocessing.Process""" """I hope it helps""" #Doesnt work in sololearn import time import multiprocessing as mp def controller(func,stoptime): thr = mp.Process(target=func) thr.daemon=True thr.start (); time.sleep (stoptime) thr.terminate () print("Process terminated") def mythread (): print ("Thread Starts") time.sleep (5) #Sleeps for 5 second print("Thread Ends") prog = mp.Process (target=controller,args=[mythread,3]) #Terminates in 3 seconds prog.start ()
11th Jan 2020, 8:39 AM
Sarthak
Sarthak - avatar
+ 1
Do you want to stop a thread when its sleeping for n seconds?
11th Jan 2020, 7:46 AM
Sarthak
Sarthak - avatar
0
In Python, you simply cannot kill a Thread. Killing a thread removes any guarantees that try/finally blocks set up so you might leave locks locked, files open, etc. It is generally a bad pattern to kill a thread abruptly, in Python and in any language. You need to use platform API to kill the thread, e.g. pthread_kill, or TerminateThread. You can access such API e.g. through pythonwin, or through ctypes. Notice that this is inherently unsafe. It will likely lead to uncollectable garbage (from local variables of the stack frames that become garbage), and may lead to deadlocks, if the thread being killed has the GIL at the point when it is killed. http://net-informations.com/python/iq/kill.htm
6th Jun 2020, 6:15 AM
rahul kumar
rahul kumar - avatar