I have been using
Python debug and LLDB attach debug since I have code in Python and C++
I have run this multiple times. It looks like everytime there is an exception in the middle of debugging, the process doesn't get killed.
As a result, now after multiple runs, when I try to use LLDB Attach debug it shows multiple instances of the same file being run
Unable to know which is the latest process
Also how to kill processes that haven't terminated.
For reference, here's an image
Those processes can probably be termed as ghost processes. Those are caused from the previous run when one does not detach the lldb from it.
Manually searching for the process ID of those and killing them solved the problem.
List python related processes
ps -ef | grep python
Killing the ones you identify as ghost.
kill -9 <process-id>
As an alternative to command line, you should be able to see that ghost python process in the process explorer (command "Open Process Explorer"), and then use Ctrl+e to kill it.
The Ctrl+e is available in VSCode 1.75 (Jan. 2023), or right now in VSCode Insiders, with issue 119345 and PR 168943 delivered.
Related
Suppose that I run an MPI program involving 25 processes on 25 different machines. The program is initiated at one of them called the "master" with a command like
mpirun -n 25 --hostfile myhostfile.txt python helloworld.py
This is executed on Linux with some bash script and it uses mpi4py. Sometimes, in the middle of execution, I want to stop the program in all machines. I don't care if this is done graciously or not since the data I might need is already saved.
Usually, I press Ctrl + C on terminal of the "master" and I think it works as described above. Is this true? In other words, will it stop this specific MPI program in all machines?
Another method I tried is to get the PID of the process in the "master" and kill it. I am not sure about this either.
Do the above methods work as described? If no, what else do you suggest? Note that I want to avoid the use of MPI calls for that purpose like MPI_Abort that some other discussions here and here suggest.
I try to run a python program on my mac terminal. I want to manually terminate it by pressing "cmd + z". I openup "monitor", and I still see python-2.7 getting 8GB ram (wtf?), and using some cpu resources. How do I completely shut down this python program (I can't force quit because I also got another python program running on a different and separate terminal.
Just want to clarify my description, I ran two python programs, and in the "monitor", I see two Python-2.7 running.
I want to terminate one of the python program running in my terminal. How do I completely shut down that one (and also free up the resources that it is using).
cmd + Z puts the currently-running process into the background. Ctrl+D will cause Python to exit
You can use kill with the pid of your background process to close it
You can check for the PIDS for any jobs with:
lsof +D path/folder
and then:
kill -9 PID-number
Also, ctrl+Z while the process is running can stop it as well.
You can quit a python program by typing quit() without the quotes of course. Be sure to include the parentheses.
I have a cpu intensive script I'm running in ipython that will take several days to finish. As I'm using my computer over these days, I would ideally like to be able to pause this script using some ipython shortcut (something along the lines of ctrl+c or ctrl+z) to temporarily reclaim full computing power. Is there any way this might be done?
I'm running IPython directly in the terminal in Ubuntu 16.04.
Thanks!
Have a look at kill and signal.
Suppose you have this running in your "IPython"
import time
i = 0
while True:
print('Printing: ', i)
time.sleep(0.5)
i += 1
Now open a new terminal window and type:
kill -SIGSTOP `pgrep ipython`
If you check your IPython terminal, it has stopped printing.
If you type:
kill -SIGCONT `pgrep ipython`
Your program will continue where it was stopped.
EDIT: Please note that the characters surrounding pgrep ipython are not single quotes; they're called "backticks". You can find out how to type them for your system/keyboard layout/etc.
EDIT: The command stops and continues all IPython processes. In reality, if you want to stop/continue a specific process, you have to give it a PID. It's a good idea to put your program in a file and grep for that.
Using Linux (Ubuntu 16.04), running Emacs 24.5.1, Python 3.5.2, and IPython 5.1.0. I often work with python-mode, with a live IPython prompt running in a buffer. I commonly run a function or loop, and sometimes need to break it early to examine stuff, usually while exploring data.
However, the first time I press Ctrl-C, this works fine, and the IPython process goes back to its prompt. Unfortunately the second time I press Ctrl-C, invariably the process exits with the message,
Process Python[myscript.py] interrupt
Then if I C-c C-c again, a new IPython process is started in the same buffer. However, my process is dead and I can't examine the variables and have to re-run it from the beginning. This is very annoying! I've searched but can't figure out why it only happens the second time I press Ctrl-C.
One possibility is that the process is receiving EOF as well as SIGINT, however I don't know why or how to be sure, or how to stop it doing so.
Any ideas?
Edit: I should mention that it does not seem to matter whether I use Emacs in the terminal emulator or in the GUI. Usually I prefer using it in the terminal.
Use bash command trap to catch signal if your script is running on Linux.
This question extends/revives this one.
The relevance to revive this topic is due to the failure in solving the same problem with the given answers.
The bash script executes a python script embedded. Something like
#!/bin/bash
./pyscript.py
chmod +x pyscript.py permission was given.
Alternative ways to run the script were used.
(python -u pyscript.py or /usr/bin/python pyscript.py)
As the title states the python program does not exit.
I have tried the following attempts within the python script to solve the issue:
sys.exit(0); %the program catches the correct exception
os._exit(1) %does not work and the correct exception is catched
sys.stdout.flush() %to clean the buffer of the stdout
The daemon solution is not suitable for what I need, because running in the background independently from the main script will not wait for the execution of the python program untill the end.
What are the alternative solutions that remain for this case?
Have you tried to use strace -p $PID on the python process? The output will not always be useful however.
From the code perspective, in addition to threads I would check if there are any signal handlers (which maybe do not terminate for some reason).
As far as threads are concerned, you might be interested in this, although I believe someone mentioned it in the other thread.
Finnally the problem is solved.
The program in python wich I've been trying to kill the process runs with multiple threads.
sys.exit(0) only terminates the thread in which the program is called.
The os._exit(1) was called with the sys.exit(0) before its execution (fail!).
By running os._exit(1) without sys.exit(0) before, the program exit the python script.
The reason must be that sys.exit() only terminates the thread in which it is called and allows the program to clean resources, while os._exit() does an abrupt program termination.
Found here.
With this solution it's better guarantee the termination of any task the program should end and then call os._exit.
what I usually do to separate a script from the main shell terminal process is sending the script inside a screen session detached. Then I kill the pid of the script without any trouble.
But for this particular case I want the program waiting for the end of the python subscript and not as a parallel process.
Also, you might want to try the trace module, i.e. running your program with #!/usr/bin/env python -m trace --trace. If python is executing some of your code (which it probably is), it should show you details on that.