Форум сайта python.su
Помогите пожалуйста! Как правильно закрыть сокет, чтобы поток завершился?
class WebSocket(threading.Thread):
users={}
queries = deque()
server=0
address=0
port=0
connections=0
stopkey = False
def __init__(self, address, port, connections):
threading.Thread.__init__(self)
logging.debug('W.%s',self.getName())
self.address = address
self.port = port
self.connections = connections
def run(self):
self.server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
self.server.bind ( ( self.address, self.port ) )
self.server.listen ( self.connections )
logging.debug('W.start wsserver ')
while True:
if(self.stopkey):
break
else:
conn,address = self.server.accept()
self.add_user(conn,address)
def stop(self):
for user in self.users.itervalues():
logging.debug('W.stopping user %s', user)
user.stop()
logging.debug('W.stop wsserver ')
self.stopkey = True
self.server.shutdown(socket.SHUT_RDWR)
self.server.close()
class WebSocketThread(threading.Thread):
stopkey = False
def __init__ ( self, conn,adress,websocket,user_id ):
self.conn = conn
self.adress = adress
self.websocket = websocket
self.user_id = user_id
threading.Thread.__init__( self )
logging.debug('W.user %s',self.getName())
def run ( self ):
self.handshake()
while True:
if(self.stopkey):
logging.debug('WS.stop user %s',self.user_id )
self.conn.close()
break
else:
self.interact()
def stop(self):
self.stopkey = True
def interact(self):
data = self.recv_data(512)
data = data.decode('utf-8', 'ignore')
data_split = data[1:].split(",")
logging.debug(data_split)
if(data_split[0]=="WORK"):
query = ['W',self.user_id]
self.websocket.query(query)
if(data_split[0]=="RESULT"):
query = ['R',[string.atoi(data_split[1],10),string.atoi(data_split[2],10)]]
self.websocket.query(query)
if(data_split[0]==""):
self.stop()
Отредактировано (Июнь 5, 2011 01:42:17)
Офлайн