Kill a thread from another process

I know that the correct way to kill a subclass of Thread is to periodically check if some flag (for example, self.running ) is set to a specific value of "kill", but I have Thread that may hang waiting for input, and I ' d would like to kill him anyway from an external process.

Any help?

+4
source share
1 answer

If you want to switch from a streaming module to a multiprocessing module for your stream interface, then this is possible. All that needs to be done is to keep track of the PID of each thread / process to start.

  from multiprocessing import Process import os,time class myThread(Process): def __init__(self): Process.__init__(self) def run(self): while True: os.system("sleep 5") if __name__ == '__main__': p = myThread() p.start() print "Main thread PID:",os.getpid() print "Launched process PID:",p.pid os.kill(p.pid,1) p.join() 
+1
source

Source: https://habr.com/ru/post/1392483/


All Articles