4545import time
4646
4747from ws4py import format_addresses
48+ from ws4py .compat import py3k
4849
4950logger = logging .getLogger ('ws4py' )
5051
@@ -161,7 +162,10 @@ def __len__(self):
161162 return len (self .websockets )
162163
163164 def __iter__ (self ):
164- return self .websockets .itervalues ()
165+ if py3k :
166+ return iter (self .websockets .values ())
167+ else :
168+ return self .websockets .itervalues ()
165169
166170 def __contains__ (self , ws ):
167171 fd = ws .sock .fileno ()
@@ -241,8 +245,8 @@ def run(self):
241245
242246 ws = self .websockets .get (fd )
243247
244- if ws :
245- if not ws .terminated and not ws . once ():
248+ if ws and not ws . terminated :
249+ if not ws .once ():
246250 with self .lock :
247251 fd = ws .sock .fileno ()
248252 self .websockets .pop (fd , None )
@@ -260,7 +264,7 @@ def close_all(self, code=1001, message='Server is shutting down'):
260264 """
261265 with self .lock :
262266 logger .info ("Closing all websockets with [%d] '%s'" % (code , message ))
263- for ws in self . websockets . itervalues ( ):
267+ for ws in iter ( self ):
264268 ws .close (code = 1001 , reason = message )
265269
266270 def broadcast (self , message , binary = False ):
@@ -274,10 +278,14 @@ def broadcast(self, message, binary=False):
274278 """
275279 with self .lock :
276280 websockets = self .websockets .copy ()
281+ if py3k :
282+ ws_iter = iter (websockets .values ())
283+ else :
284+ ws_iter = websockets .itervalues ()
277285
278- for ws in websockets . itervalues () :
286+ for ws in ws_iter :
279287 if not ws .terminated :
280288 try :
281289 ws .send (message , binary )
282- except Exception , ex :
290+ except :
283291 pass
0 commit comments