6868
6969"""
7070
71+ import threading
7172import urllib2
7273import httplib
7374import socket
@@ -89,19 +90,28 @@ def close_connection(self, host):
8990
9091 def open_connections (self ):
9192 """return a list of connected hosts"""
92- return self ._connections .keys ()
93+ retVal = []
94+ currentThread = threading .currentThread ()
95+ for thread , host in self ._connections .keys ():
96+ if thread == currentThread :
97+ retVal .append (host )
98+ return retVal
9399
94100 def close_all (self ):
95101 """close all open connections"""
96- for host , conn in self ._connections .items ():
102+ for _ , conn in self ._connections .items ():
97103 conn .close ()
98104 self ._connections = {}
99105
100106 def _remove_connection (self , host , close = 0 ):
101- if self ._connections .has_key (host ):
102- if close : self ._connections [host ].close ()
103- del self ._connections [host ]
104-
107+ key = self ._get_connection_key (host )
108+ if self ._connections .has_key (key ):
109+ if close : self ._connections [key ].close ()
110+ del self ._connections [key ]
111+
112+ def _get_connection_key (self , host ):
113+ return (threading .currentThread (), host )
114+
105115 def _start_connection (self , h , req ):
106116 try :
107117 if req .has_data ():
@@ -132,7 +142,8 @@ def do_open(self, http_class, req):
132142
133143 try :
134144 need_new_connection = 1
135- h = self ._connections .get (host )
145+ key = self ._get_connection_key (host )
146+ h = self ._connections .get (key )
136147 if not h is None :
137148 try :
138149 self ._start_connection (h , req )
@@ -155,7 +166,7 @@ def do_open(self, http_class, req):
155166 if need_new_connection :
156167 if DEBUG : print "creating new connection to %s" % host
157168 h = http_class (host )
158- self ._connections [host ] = h
169+ self ._connections [key ] = h
159170 self ._start_connection (h , req )
160171 r = h .getresponse ()
161172 except socket .error , err :
0 commit comments