-1

I am trying to start a thread using python threading module. I want to kill this newly created thread under certain conditions. I have written below code snippet but this seems to hang and process never returns

from threading import Thread
import time
import os, signal
def sum():
    print("ada")
    time.sleep(5)
    print("ada1")

current = Thread(target=sum)
current.start()
current.join()
cur_pid = os.getpid()
print(cur_pid)
os.kill(cur_pid, signal.SIGSTOP)
print(current)
Nitesh
  • 1,715
  • 3
  • 27
  • 47
  • Do you want to kill the newly created process or the program itself? – Max Mar 25 '20 at 09:06
  • @Max: I want to kill newly created process – Nitesh Mar 25 '20 at 09:08
  • 1
    Does this answer your question? [How to stop a looping thread in Python?](https://stackoverflow.com/questions/18018033/how-to-stop-a-looping-thread-in-python) – Max Mar 25 '20 at 09:25

1 Answers1

-1

You'll want to send SIGKILL or SIGTERM, not SIGSTOP.

SIGSTOP pauses the process (which sounds like what you're seeing).

AKX
  • 93,995
  • 11
  • 81
  • 98
  • Problem is SIGKILL terminates the whole script. my last code line is never executed. I only want to kill this particular thread which is calling function – Nitesh Mar 25 '20 at 09:12
  • With os.getpid() he is getting the PID of the actual program that created the thread, he is stopping the program instead of the thread – Max Mar 25 '20 at 09:13