views:

44

answers:

1

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.

A: 

It sounds like you are invoking close() in one thread while another thread is executing perform(). Luckily, the library warns you rather than descending into unknown behavior-ville.

You should only use the curl session from one thread - or have the perform() thread somehow communicate when the call to perform() is complete.

Jeremy Brown
I am using a signal sent to the thread containing the pycurl connection to initiate the self.conn.close() call. The issue is, barring errors and timeouts, perform() never is complete, information will keep streaming until either party closes the connection (I am interfacing with the twitter stream api, if that helps at all).
boldfield