user739807
user739807

Reputation: 855

How to Retrieve an object from memory

I have a job that has been running for hours. Towards the end i get an exception, at the point before the results are made perssistent. However all intermediate results have been saved to a dictionary object, whcih i assume is in memory. Is there a way of accessing this object?

This is the sample code

def create_rgr(frames):
    new_frames ={}
    for radec in frames.keys():
        for rframe in frames[radec]:
            rgr=subrgr(rframe,radec, store=1)
            new_frames.setdefault(radec,[]).append(rgr)

     ##Exception thrown before this point ###########

    tools.save(new_frames)

Upvotes: 0

Views: 146

Answers (3)

glglgl
glglgl

Reputation: 91159

If you have started this process from the command line, you can examine the traceback.

>>> def a(x): y=x; 0/0
...
>>> def b(x): a(x)
...
>>> try: b(100)
... except: import sys; e=sys.exc_info()
...
>>> e
(<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo by zero',), <traceback object at 0x00B4B670>)
>>> e[2]
<traceback object at 0x00B4B670>
>>> dir(e[2])
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'tb_frame', 'tb_lasti', 'tb_lineno', 'tb_next']
>>> e[2].tb_next
<traceback object at 0x00B4B648>
>>> e[2].tb_next.tb_next
<traceback object at 0x00B4B5F8>
>>> e[2].tb_next.tb_next.tb_next
>>> e[2].tb_next.tb_next
>>> e[2].tb_next.tb_next.tb_frame
<frame object at 0x00B88060>
>>> dir(e[2].tb_next.tb_next.tb_frame)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'f_back', 'f_builtins', 'f_code', 'f_exc_traceback', 'f_exc_type', 'f_exc_value', 'f_globals', 'f_lasti', 'f_lineno', 'f_locals', 'f_restricted', 'f_trace']
>>> e[2].tb_next.tb_next.tb_frame.f_locals
{'y': 100, 'x': 100}

So here you have the values of all variables in the call stack. AFAIK they remain here untik the next exception.

Upvotes: 0

Janne Karila
Janne Karila

Reputation: 25207

If you want to ensure the results are stored even in the case of an exception, you can use a finally block:

def create_rgr(frames):
    new_frames ={}
    try:
        for radec in frames.keys():
            for rframe in frames[radec]:
                rgr=subrgr(rframe,radec, store=1)
                new_frames.setdefault(radec,[]).append(rgr)

         ##Exception thrown before this point ###########
    finally:   
        tools.save(new_frames)

Upvotes: 2

NPE
NPE

Reputation: 500963

I am not 100% sure I follow.

If the long-running Python process has terminated as the result of the exception then no, you can't get to the data that the now-defunct process had in memory.

If the Python process is still running, and you can feed commands into it (or can add commands to the script and re-run), then look at the contents of the dictionary that you mention.

What am I missing?

Upvotes: 0

Related Questions