0

I am currently running a program, which i expect to go on for an hour or two. I need to break out of the loop right now, so that rest of the program continues.

This is a part of the code:

from nltk.corpus import brown
from nltk import word_tokenize, sent_tokenize
from operator import itemgetter

sentences = []
try:
    for i in range(0,55000):
        try:
            sentences.append(brown.sents()[i])
            print i
        except:
            break
except:
    pass 

the loop is currently around 30,000. I want to exit and continue with the code (not shown here). Please suggest me how to such that, the program doesn't break exit completely. (Not like keyboard interrupt)

3 Answers3

4

Since it is already running, you can't modify the code. Unless you invoked it under pdb, you can't break into the Python debugger to alter the condition to leave the loop and continue with the rest of the program. So none of the normal avenues are open to you.

There is one outside solution, which requires intimate knowledge of the Python interpreter and runtime. You can attach the gdb debugger to the Python process (or VisualStudio if you are on Windows). Then when you break in, examine the stack trace of the main thread. You will see a whole series of nested PyEval_* calls and so on. If you can figure out where the loop is in the stack trace, then identify the loop. Then you will need to find the counter variable (an integer wrapped in a PyObject) and set it to a large enough value to trigger the end of the loop, then let the process continue. Not for the faint of heart! Some more info is here:

Realistically, you just need to decide if you either leave it alone to finish, or kill it and restart.

It's probably easiest to simply kill the process, modify your code so that the loop is interruptible (as @fedorSmirnov suggests) with the KeyboardInterrupt exception, then start again. You will lose the processing time you have invested already, but consider it a sunken cost.

There's lots of useful information here on how to add support to your program for debugging the running process:

Community
  • 1
  • 1
gavinb
  • 19,278
  • 3
  • 45
  • 60
3

I think you could also put the for loop in a try block and catch the keyBoardInterrupt exception by proceeding with the rest of the program. With this approach, you should be able to break out of the loop by hitting ctrl + C while staying inside your program. The code would look similar to this:

try:

    # your for loop

except KeyboardInterrupt:

    print "interrupted"

# rest of your program
fedorSmirnov
  • 701
  • 3
  • 9
  • 19
2

You can save the data with pickle before the break command. Next time load the data and continue the loop.

Bardia Heydari
  • 777
  • 9
  • 24