Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a python script without losing data

Tags:

python

We have been running a script on partner's computer for 18 hours. We underestimated how long it would take, and now need to turn in the results. Is it possible to stop the script from running, but still have access to all the lists we are building?

We need to add additional code to the one we are currently running that will use the lists being populated right now. Is there a way to stop the process, but still use (what has been generated of) the lists in the next portion of code?

My partner was using python interactively.


update

We were able to successfully print the results and copy and paste after interrupting the program with control-C.

like image 266
CuriousGeorge119 Avatar asked Apr 18 '18 16:04

CuriousGeorge119


People also ask

How do you end a Python program without killing it?

To stop a script in Python, press Ctrl + C. If you are using Mac, press Ctrl + C. If you want to pause the process and put it in the background, press Ctrl + Z (at least on Linux). Then, if you want to kill it, run kill %n where “n” is the number you got next to “Stopped” when you pressed Ctrl + Z.

How do I stop an entire Python script?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.

How do I stop a Python program after a certain time?

terminate() function will terminate foo function. p. join() is used to continue execution of main thread. If you run the above script, it will run for 10 seconds and terminate after that.


1 Answers

Well, OP doesn't seem to need an answer anymore. But I'll answer anyway for anyone else coming accross this.
While it is true that stopping the program will delete all data from memory you can still save it. You can inject a debug session and save whatever you need before you kill the process.

Both PyCharm and PyDev support attaching their debugger to a running python application.
See here for an explanation how it works in PyCharm.
Once you've attached the debugger, you can set a breakpoint in your code and the program will stop when it hits that line the next time. Then you can inspect all variables and run some code via the 'Evaluate' feature. This code may save whatever variable you need.

I've tested this with PyCharm 2018.1.1 Community Edition and Python 3.6.4.

In order to do so I ran this code which I saved as test.py

import collections
import time

data = collections.deque(maxlen=100)
i = 0
while True:
    data.append(i % 1000)
    i += 1
    time.sleep(0.001)

via the command python3 test.py from an external Windows PowerShell instance. Then I've opened that file in PyCharm and attached the debugger. I set a Breakpoint at the line i += 1 and it halted right there. Then I evaluated the following code fragment:

import json
with open('data.json', 'w') as ofile:
    json.dump(list(data), ofile)

And found all entries from data in the json file data.json.


Follow-up:
This even works in an interactive session! I ran the very same code in a jupyter notebook cell and then attached the debugger to the kernel. Still having test.py open, I set the breakpoint again on the same line as before and the kernel halted. Then I could see all variables from the interactive notebook session.

like image 180
swenzel Avatar answered Sep 21 '22 22:09

swenzel