有什么方法可以从正在运行的函数中检索局部变量吗? [英] Is there any way to retrieve a local variable from a running function?

查看:76
本文介绍了有什么方法可以从正在运行的函数中检索局部变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绝望.说我们有以下内容:

Desperate. Say we have the following:

def main():
 ALotOFCode
 list1 = []
 list2 = []
 while condition:
  # a lot of times where raw_input is used in this loop
  # e.g. 
  x = raw_input('lots of steps to compute x')
  y = raw_input('lots of steps to compute y')  
  list1 = list1.append(x)
  list2 = list2.append(y)
  stream.write({'x':list1,'y':list2}) #send new data point to plot.ly via raspberry pi

我不知道发生了什么.但是我在plot.ly中的情节消失了.完全删除.到目前为止,我一直在弄乱自己在PC上的绘图内容,然后继续输入数据并从树莓派创建绘图.我可以看到正在构建的情节没有问题.然后,我回到我的电脑上,然后单击刷新.绘图,所有数据都消失了.回到树莓.已同步.走了.

I don't know what happened. But my plot in plot.ly is gone. Deleted completely. I was messing with what I had on the plot thus far on my PC and then I continued inputting data and building the plot from raspberry pi. I could see the plot being built no problem. Then I went back to my PC and hit refresh. Plot and all data gone. Went back to raspberry. Already synced. Gone.

虽然我现在正在写list1和list2,但我知道它包含了重新绘制图所需要的所有数据.但是,有什么办法可以访问它并将其保存到文件中?我的python shell当前正在等待通过raw_input的输入,因此我无法使用该shell.有什么办法可以终止当前仍在运行的程序范围内的变量?显然,一旦程序结束,本地变量就会被删除.

I know though that list1 and list2 right now as I am writing this contain all the data I need to remake my plot. But is there any way at all to access it and save it to file? My python shell is currently waiting for input via raw_input so I cant use the shell. Is there any way I can terminal my way into the variables currently in the scope of the still running program? Obviously once the program ends the local variables are deleted.

当然,如果在plot.ly上为每个图保存了历史记录,这会有所帮助,但我找不到恢复到先前状态的任何选项.

Of course if a history is saved for each plot on plot.ly that would help to but I cant find any restore to previous state option.

更新:所以-谢谢上帝-我已经将一些中间步骤的输出保存到了文件中.我有效地放弃了尝试访问变量的方法,而是尝试通过新的即时脚本生成list1list2.早上8:30,我有了我的数据,而在10:00,我的主管高兴地看着它,这是不明智的.尽管还没有人给出明确的答案或说明如何直接访问list1list2是不可能的,但这个问题仍然悬而未决. (我确实查看了评论中的每个建议,但找不到能提供答案的任何内容)

Update: So - thank god - I had saved the outputs of some of my intermediate steps to file. I effectively gave up trying to access the variable and instead tried generating list1 and list2 through a new on-the-fly script. At 8:30am in the morning I had my data and at 10am my supervisor happily looked at it none the wiser. This question remains open though as yet nobody has given a clear answer or explained how accessing list1 and list2 directly isn't possible. (I did go through each suggestion in the comments but couldn't find anything that provided an answer)

最后的强制性评论:我对Plot.ly的信念已经瓦解了,至少有一段时间了.

Obligatory final comment: My faith in Plot.ly has been shattered, at least for a while.

推荐答案

为了能够读取"内部变量以进行调试,我看到以下想法:

To be able to "read" internal variables for debug purpose I see following ideas:

  1. 创建一个日志文件,其中每个变量更改都在每个阻塞功能之前一行.即使日志很大.然后在Raspberry(新的控制台或新的远程连接)上使用"tail"跟踪日志.

  1. create a log file with a line for each variable change and before each blocking function. Even if the log is huge. And then follow log with "tail" on Raspberry (new console or new remote connection).

将变量转换为全局变量,并添加一些代码,当按下特殊键时,会将所有变量输出到标准输出,例如Ctrl + C几乎中断了所有操作. 如果无法将变量转换为全局变量(例如,由于嵌套调用的原因),请创建保存最后一个已知值的新变量.

Transform variables to be global and add some code which ouputs all variables to standard output when a special key is hit, for example Ctrl+C which interrupts almost everything. If transforming variables to global isn't possible (due to nested calls reasons for example), create new variables which holds last known values.

Ctrl + C处理可以这样完成(来自此处):

Ctrl+C handling could be done like that (from here) :

#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

如您所见,我的选项意味着修改代码以使其易于使用或可查询.

As you see, my options implies to modifiy the code to make it chatty or queryable.

这篇关于有什么方法可以从正在运行的函数中检索局部变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆