Pausing an IPython script with a shell command? - python

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.

Related

How to start a new console and run a command in it programmatically?

I'm running tons of experiments and I'm tired of manually opening a terminal and typing:
!tensorboard --logdir="C:\Users\OneDrive \Pycharm\DANN MNISTM SVHN\tmp\1561358957553" --host localhost --port 9353
to start TensorBoard. I want to do this programmatically.
I want to do this in Pycharm, or even Jupyter. But, I want this to be done in a new notebook / terminal in case of Jupyter, not the one I'm currently running cause this will hog the terminal and prevents me from doing extra processing. Same thing in case of Pycharm, I want the command above to be run in a new IPython console / terminal. Is there a Python/IPython way to do that?
Here is what I tried:
import os
os.startafile('cmd')
But I don't know how to write commands to that newly created window without going to it manually.
I also tried subprocess Popen, but it didn't work, in particular, I created a baby process using Popen but when I call communicate method and send a command it waits for a response! I don't want to wait, there is no any response. I just want to move on to next command.
I solved it, but I'm not satisifed.
I used os.popen('the command'). It is working fine, but, it keeps sending back updates to my interactive consonle, this clutters it with useless wiritings, tons of them.

Cmd + z doesn't shut down python

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.

VSCode kill running processes

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.

Emacs: How do I stop my Python process from being killed by Ctrl-C

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.

Hanging script and cmd won't close

I am trying to run a python script via cmd prompt on my work PC (Windows 7, Python 2.7). The script requires filepaths from different drives on my PC. I am correctly pulling all necessary filepaths and I press Enter to run the script but the script just hangs. The only thing that shows is a blinking underscore. I try to click the X to close the prompt but nothing happens.
I am not able to Ctrl+C out of the program either. I open up Task Manager and I am not able to End Task (nothing happens) or End Process (cmd.exe doesn't even show up in this tab). I also tried Start-->Run-->taskkill /im cmd.exe but nothing happens. The rest of my team has no problem with Python 2.7. The only way to get out of the frozen cmd is to hold down the power button. I do not want to have to keep going through this process especially since this is during work. I'm hoping someone will be able to help me out:
Any idea what's wrong with the version of Python I am using?
How am I able to kill cmd.exe so that I can continue normal work functions without having to hold down the power button and waiting 5-10 minutes to reboot my PC?
The filepaths I was pulling files from were through ClearCase directories. It turned out that I simply had to reinstall ClearCase. There must have been some configuration issue that was causing the cmd to hang and thus forcing a hard reboot. It's good to point out that I am no longer experiencing this problem and that nothing was wrong with the python scripts.

Categories