Failed to close thread opened with pycurl

I am working on a client for a web service using pycurl. The client opens a connection to the streaming service and creates it in a separate stream. Here the version of the connection configuration is trimmed:

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 that I want to close the connection, if I call

 self.conn.close() 

I get the following exception:

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

Which, in a sense, makes sense, the connection is constantly open. I hunted around and could not find a way around this problem and completely close the connection.

+4
source share
2 answers

You obviously showed some methods from the curl wrapper class, you need to make the object process itself.

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

and do not explicitly call closure. When the object completes its task and all references to it are deleted, the curl connection will be closed.

0
source

It looks like you are calling close () on one thread, and the other thread is performing the perform () function. Fortunately, the library warns you, rather than descending into unknown ville behavior.

You should use only a curling session from one thread - or the thread () thread somehow exchanges when the call to the execute () function ends.

+1
source

All Articles