3

I am using Pycharm Community 4.5.4. I am running a program that takes a very long time to complete.

Unfortunately, I have a print statement that comes before the line that saves the data accumulated which has an error. I ran the print line by itself and confirmed that the line will fail.

It is possible that I made the change after I ran the program and so it won't fail but I can't be sure. The portion of code takes about 90 hours to complete and I have about 30 left if the execution time stays around the average.

So, if it fails, is there any way to recover the data?

Just to be clear, I am running and not debugging. Also, I am line profiling to see how each 2.5 hour iteration's time is spent.

Chris
  • 431
  • 5
  • 16
  • 1
    if you have run this in a console, you can use `pdb.pm()` to break into the failing context (don't forget `import pdb` first - always annoying when I forget, and now the last error is that it didn't know what pdb was :) and look at things, save, etc. Or (I don't know pycharm) you may be able to put a breakpoint there. If you ran it standalone though, there's no way. – Corley Brigman Sep 08 '15 at 15:13
  • 2
    You can use http://stackoverflow.com/questions/242485/starting-python-debugger-automatically-on-error in your main process. And use https://www.jetbrains.com/pycharm/help/attaching-to-process.html to attach to the main process. – Ali SAID OMAR Sep 08 '15 at 15:27
  • Are you also writing a log file along with the program? – Steven Correia Sep 08 '15 at 15:41
  • @AliSAIDOMAR I was able to use the 'Attach to process...' and then use the 'Evaluate Code Fragment' (Alt+F8) to import additional modules (Pickle) and dump the value of a variable at a breakpoint that I set. Will you submit your comment as an answer so I can give you the credit? Thanks soooo much. – Chris Sep 08 '15 at 15:44

1 Answers1

2

So I told, you shoud update your main module to exit on the breakpoint

import pdb
import random
import time
import os
import traceback
import sys


def main(*args, **kw):
    for i in range(10):
        print("OK I'm in %d" % os.getpid())
        time.sleep(4)
        if i == 9:
            raise Exception

if __name__ == '__main__':
    try:
        main()
    except:
        type, value, tb = sys.exc_info()
        traceback.print_exc()
        pdb.post_mortem(tb)

Starting python debugger automatically on error Credit.

And use Pycharm attach to process. if an error is raised so you will be able to debug with pycharm.

Community
  • 1
  • 1
Ali SAID OMAR
  • 6,404
  • 8
  • 39
  • 56
  • I should add that in the OP the program in question is actually still running. This situation is rare, but the program will run for ~90 hours. This means that I would need to break into a currently running program and be able to access the data. Using the information you left in the comment above, I was able to do this. Your answer works great if I could start over but the whole point was to avoid starting over and losing the data. It is possible, like I said in the comment above, to attach to a currently running process and use Run | Evaluate Expression... to import, save, or whatever. – Chris Sep 08 '15 at 16:22