Search code examples
pythonmultithreadingpycurl

Unable to close a stream opened with pycurl


I am working on a client for a web service using pycurl. The client opens a connection to a stream service and spawns it into a separate thread. Here's a stripped down version of how the connection is set up:

def _setup_connection(self):
    self.conn = pycurl.Curl()
    self.conn.setopt(pycurl.URL, FILTER_URL)
    self.conn.setopt(pycurl.POST, 1)
    .
    .
    .
    self.conn.setopt(pycurl.HTTPHEADER, headers_list)
    self.conn.setopt(pycurl.WRITEFUNCTION, self.local_callback)

def up(self):
    if self.conn is None:
        self._setup_connection()
    self.perform()

Now, when i want to shut the connection down, if I call

self.conn.close()

I get the following exception:

error: cannot invoke close() - perform() is currently running

Which, in some way makes sense, the connection is constantly open. I've been hunting around and cant seem to find any way to circumvent this problem and close the connection cleanly.


Solution

  • You obviously showed some methods from a curl wrapper class, what you need to do is to let the object handles itself.

    def __del__(self):
        self.conn.close()
    

    and don't call the closing explicitly. When the object finishes its job and all the references to it are removed, the curl connection will be closed.