3

I have a multi threaded program with the flow as below:

Main program:

#Listen to the signal
signal.signal(signal.SIGINT, some_function)

#Actual logic by calling some other function, which spawns multiple threads
try:
   core_logic()  <--- any exception here will invoke the cleanup_function()
except:
   cleanup_function()

some_function code

def some_function():
    #kill the states of the sub-threads generated by main core_logic()
    os.kill(processid, sigint)

cleanup code:

def cleanup_function():
    #send a kill to the main program using os.getpid()

I'm facing problems with the cleanup. When I invoke the main program and when an exception is thrown, the signal is sent correctly and the main program gets the SIGINT, but all the sub-threads are not terminated correctly. What mistake am I doing. Should I use SIGTERM instead? Will that help in killing all the sub-threads and child process spawned?

Sandy
  • 187
  • 3
  • 16
  • What do you mean by "sub-threads are not terminated correctly"? – dhke May 28 '15 at 09:50
  • The files or directories created by the sub-threads are still in USE and i'm not able to delete them. – Sandy May 28 '15 at 10:08
  • You expect SIGINT to automatically terminate running threads so that they stop running before your cleanup function runs? – dhke May 28 '15 at 10:28
  • Yes. Because in the some_function() when the signal is listened, I'm iterating through the processes spawned and terminating them using terminate() – Sandy May 28 '15 at 11:07
  • I sense significant confusion here. The question talks about *threads*, but now you are talking about *processes* holding resources. These two --especially in python-- are fundamentally different. If using processes: `terminate()` just signals termination, it does not *wait* for termination. Use `join()` to wait for actual termination. – dhke May 28 '15 at 11:18
  • Yes. I'm doing it. To be more clear i'm doing process.terminate() and then process.join(). – Sandy May 28 '15 at 11:25
  • Please update your question with *all relevant* information. It is obvious that the problem is not adequately described by the information you are currently giving. – dhke May 28 '15 at 11:29
  • http://stackoverflow.com/questions/32673497/signal-not-handled-when-multiple-threads and http://stackoverflow.com/questions/25676835/signal-handling-in-multi-threaded-python may be helpful – BAE Sep 20 '15 at 16:49

0 Answers0