解决 paramiko 上的线程清理问题

发布于 2024-08-11 04:55:03 字数 583 浏览 8 评论 0原文

我有一个使用 paramiko 的自动化流程,并出现此错误:

Exception in thread Thread-1 (most likely raised during interpreter 
shutdown)

....
....
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 
'error' 

我知道这是清理/线程中的问题,但我不知道如何修复它。

我有最新版本(1.7.6)并根据 this thread,解决了,直接下载代码还是报错。

该故障发生在winxp/win2003下的Python 2.5/2.6上。

我在 __del__ 析构函数中关闭连接,然后在脚本结束之前关闭它,但这些都不起作用。还有更多吗,使用这个错误发生得更早,所以可能与解释器关闭无关?

I have an automated process using paramiko and have this error:

Exception in thread Thread-1 (most likely raised during interpreter 
shutdown)

....
....
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 
'error' 

I understand that is a problem in the cleanup/threading, but I don't know how to fix it.

I have the latest version (1.7.6) and according to this thread, it was solved, so I download the code directly but still get the error.

The failure occurs on Python 2.5/2.6 under winxp/win2003.

I close the connection in the __del__ destructor, then close it before the end of the script, none of which works. Is there more, using this the error happened earlier, so maybe is not related to interpreter shutdown??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假装爱人 2024-08-18 04:55:03

__del__ 不是解构函数。当您删除对象的姓氏时会调用它,而退出解释器时不一定会发生这种情况。

任何管理上下文的东西(例如连接)都是上下文管理器 例如,有关闭< /a>:

with closing(make_connection()) as conn:
    dostuff()

# conn.close() is called by the `with`

无论如何,发生此异常是因为您有一个守护线程仍在尝试执行其工作,而解释器已经关闭。

我认为你只能通过编写 停止的代码来解决这个问题退出之前所有正在运行的线程

__del__ is not a deconstructor. It's called when you delete a object's last name, which doesn't nessesarily happen when you exit the interpreter.

Anything that manages a context, such as connections, is a context manager For example there is closing:

with closing(make_connection()) as conn:
    dostuff()

# conn.close() is called by the `with`

Anyways, this exception happens because you have a daemonic thread that is still trying to do it's work while the interpreter is already shutting down.

I think you can only fix this by writing code that stops all running threads before exiting.

够运 2024-08-18 04:55:03

在正常的程序控制流中关闭连接,而不是在 __del__ 中,正如 @THC4k 所说,它不是解构函数,一般来说,您不应该需要使用 < code>__del__(当然也有例外)。

如果您正在创建自己的线程,并且希望它们在主线程退出时正常退出,则需要 .setDaemon(True) 。

Close your connections in the normal program control flow, not in __del__, as @THC4k said, it's not a deconstructor, and in general, you shouldn't need to use __del__ (of course there are exceptions).

If you're creating your own threads, you need to .setDaemon(True) if you want them to exit normally when the main thread exits.

猫弦 2024-08-18 04:55:03

我现在,却不是这样。但是找到这个讨论,搜索我的 wxpython 应用程序的问题。

解决它,向主框架添加关闭事件。所以所有线程都会关闭。

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MyFrame, self).__init__(*args, **kwargs)

        # Attributes
        self.panel = MainPanel(self)

        # Setup
        path = os.path.abspath("./comix.png")
        icon = wx.Icon(path, wx.BITMAP_TYPE_PNG)
        self.SetIcon(icon)

        # Layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.CreateStatusBar()
        # Event Handlers
        self.Bind(wx.EVT_CLOSE, self.OnClose)

   def OnClose(self, event):
        ssh.close()
        winssh.close()
        event.Skip()

我希望这对任何人都没有帮助。

I now, is not the case. But a find this discussion, searching a problem whit my wxpython app.

Solve it to add a close event to the main frame. So all the thread's will be close.

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MyFrame, self).__init__(*args, **kwargs)

        # Attributes
        self.panel = MainPanel(self)

        # Setup
        path = os.path.abspath("./comix.png")
        icon = wx.Icon(path, wx.BITMAP_TYPE_PNG)
        self.SetIcon(icon)

        # Layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.CreateStatusBar()
        # Event Handlers
        self.Bind(wx.EVT_CLOSE, self.OnClose)

   def OnClose(self, event):
        ssh.close()
        winssh.close()
        event.Skip()

I hope this cant help to anyone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文