Python - Problem closing threads
<p>I'm using urllib2 to monitor a web socket. I have the following class which handles connecting to the site and receiving a response:</p>

<pre><code>class Stream(Thread):

    def __init__(self, connectCallback=None, **kwargs):
        self.connectCallback = connectCallback
        Thread.__init__(self)

    def run(self):
        self.running = True

        while self.running:
            try:
                req = urllib2.Request(url, headers=self.headers)
                req.add_data(self.body)
                print "Stream connecting %s" % self
                self.connectCallback(self)
                f = urllib2.urlopen(req)
                print "Stream connected %s" % self
                while 1:
                    if self.running:
                        print "data receiving"
                    else:
                        f.close()
                        break
                retry_time = self.retry_time
            except (urllib2.URLError, urllib2.HTTPError), e:
                print 'Exception: %s, retry in %f' % (e, retry_time)
                time.sleep(retry_time)
                retry_time *= 4.0
                if retry_time &gt; self.max_retry:
                    retry_time = self.max_retry
            except Exception as err:
                print 'Exception: %s' % err

        print "Stream closed %s" % self

    def disconnect(self):
        print 'Disconnecting stream %s ' % self
        if self.running is False:
            return
        self.running = False
        self.join()
</code></pre>

<p>I want to be able to run more than one instance of my Stream class asynchronously. Here is my main class which instantiates the Streams:</p>

<pre><code>class Consumer(object):

    def __init__(self):
        try:
            self.producer = self._open_stream()
            while 1:
                sleep(100)
        except KeyboardInterrupt:
            self.producer.disconnect()
            sys.exit()

    def onStreamConnect(self, instance):
        print "STREAM CONNECTED %s " % instance
        if getattr(self, 'new_producer', None):
            self.producer.disconnect()
        self.producer = instance

    def onTrackChange(self):
        print "TRACKS UPDATED"
        self.new_producer = self._open_stream()

    def _open_stream(self):
        s = Stream(onStreamConnect)
        s.start()
        return s

if __name__ == "__main__":
    c = Consumer()
</code></pre>

<p>This works great when I have just one stream object, and the script closes cleanly. However, when I call my onTrackChange() method and establish more than one connection, the thread never closes and the run loop doesn't seem to end.</p>

<p>I'm sure I'm missing something obvious, but why is it I can't shut down the stream objects?</p>
