English 中文(简体)
Solving thread cleanup on paramiko
原标题:

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??

问题回答

__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.

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.

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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签