1

After I CTRL+C to kill the python program, I found most of the threads and processes has been terminated (they are watching some flags), but still some are running in background without any output to console. Is there a way that I can investigate which one is still running?

If you suggest to print something when thread/process is running, it seems not gonna work. First, some of the threads/processes are running methods in third party libraries, for example, websocket.run_forever. Second, for those threads/processing running my method, I'm pretty sure they are monitering a flag and once it's set, they all quit. For those threads/processes running third party methods, I call some methods to terminate them like websocket.close() and I do see they are terminated. So in this case, it's confusing to me what is still running.

pythonician_plus_plus
  • 1,244
  • 3
  • 15
  • 38
  • if you want to get them to stop, just restart – Untitled123 Jan 03 '16 at 04:56
  • Open the task manager on your OS? – OneCricketeer Jan 03 '16 at 07:26
  • What Control-C does depends on the shell and OS. Whether it kills a process even depends on the program itself. This makes it really hard to give an advise without a lot further info, perhaps even an example. Anyhow, how to stop running processes depends on your OS, too, but it should be trivial to find out by just searching the web. – Ulrich Eckhardt Jan 03 '16 at 14:24

2 Answers2

0

You can call this wonderful dumpstacks function, which prints the current traceback of all the running threads.

If your main thread (or another thread under your control) is still running at that point (e.g. waiting on other threads to finish before quiting), add the function call there.

Another option is to attach a pdb debugger to the running process, and then run dumpstacks.

That should give you a very good idea of what is still running.

Community
  • 1
  • 1
shx2
  • 61,779
  • 13
  • 130
  • 153
0

Here is what I finally did. It doesn't directly answer the question but it solves my problem.

First, after I CTRL + C the program, I do a

ps aux | grep -i "myProgram.py"

and find out there was only one process still running (more than five before I CTRL+C).

Next, I make all the threads I created in the program "daemon", especially those running in main process.

Then, I do

threads = threading.enumerate()
for _t in threads:
  print _t.name
  print _t.isAlive()
  print _t.isDaemon()

so as to find out anything still running after I catch the KeyboardInterrupt (from CTRL+C) and do the cleaning up (set the thread/process terminate flag).

Now the program exists gracefully after I CTRL+C and nothing keep running in background.

pythonician_plus_plus
  • 1,244
  • 3
  • 15
  • 38
  • Install `htop` when on Linux. That said, read `man pgrep`, `man killall`. Lastly, killing a process terminates all its threads, too, so you shouldn't have to worry about threads hanging around but just address the processes. – Ulrich Eckhardt Jan 04 '16 at 17:25