gpt4 book ai didi

python - 结束 ssh session 后在后台运行 python/matplotlib 的问题

转载 作者:IT老高 更新时间:2023-10-28 22:22:02 26 4
gpt4 key购买 nike

我必须通过 VPN 连接,然后从家里通过 ssh 连接到我的工作服务器,并想在后台运行 python 脚本,然后退出 ssh session 。我的脚本使用 matplotlib 制作了几个直方图,只要我保持连接打开一切都很好,但是如果我注销,我会在为脚本创建的日志文件中不断收到错误消息。

 File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 2058, in loglog
ax = gca()
File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 582, in gca
ax = gcf().gca(**kwargs)
File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 276, in gcf
return figure()
File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/pyplot.py", line 254, in figure
**kwargs)
File "/Home/eud/jmcohen/.local/lib/python2.5/site-packages/matplotlib/backends/backend_tkagg.py", line 90, in new_figure_manager
window = Tk.Tk()
File "/Home/eud/jmcohen/.local/lib/python2.5/lib-tk/Tkinter.py", line 1647, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "localhost:10.0"

我假设自从我关闭 X11 ssh session 以来,它不知道在哪里创建我想要的数字。如果我在脚本运行时登录,我看不到任何数字弹出(尽管那是因为我的脚本中没有 show() 命令),并且我认为 python 使用 tkinter 来显示数字。我创建数字的方式是,

loglog()
hist(list,x)
ylabel('y')
xlabel('x')
savefig('%s_hist.ps' %source.name)
close()

脚本需要一些初始输入,所以我在后台运行它的方式是

python scriptToRun.py << start>& logfile.log&

有没有办法解决这个问题,还是我只需要通过 ssh 进入我的机器?

谢谢。

最佳答案

我相信您的 matplotlib 后端需要 X11。查看您的 matplotlibrc 文件以确定您的默认值是什么(从错误中,我打赌 TkAgg)。要在没有 X11 的情况下运行,请使用 Agg 后端。通过将其添加到 python 程序中,可以在 matplotlibrc 文件中全局设置它,也可以通过脚本在脚本上设置它:

import matplotlib
matplotlib.use('Agg')

关于python - 结束 ssh session 后在后台运行 python/matplotlib 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2443702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com