这是我的客户:
class TestClient(asyncore.dispatcher):
#private
_buffer = ""
#public
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
self.client = Sender(self)
self.client.start()
# other code
def handle_connect(self):
pass
def handle_close(self):
self.close()
self.client.stop()
def handle_read(self):
try:
data = self.recv(8192)
self.artist.ParseFromString(data)
# print data here
except Exception as ex:
print ex
def writable(self):
return (len(self._buffer) > 0)
def handle_write(self):
sent = self.send(self._buffer)
self.buffer = self._buffer[sent:]
def link(self, name):
return name.replace(' ', '%20')
def sendArtist(self, artist):
print "here"
self._buffer = self.link(artist)
class Sender(threading.Thread):
#private
_stop = False
#public
def __init__(self, client):
super(Sender, self).__init__()
self.client = client
def stop(self):
self._stop = True
def run(self):
i = 0
while self._stop == False and i < len(artists.artistList):
self.client.sendArtist(artists.artistList[i])
i += 1
client = TestClient("127.0.0.1", 7899)
asyncore.loop()
我的问题是在类中的 run 方法中,对于调用时Sender
的每个项目,将为所有项目调用,并且仅对最后一个项目调用。artist.artistList
sendArtist()
writable()
handle_write()
我该怎么做handle_write()
才能为列表中的每个项目调用,而不仅仅是最后一个?
这就是现在的工作方式:artist.artistList = ["madonna", "tiesto", "atb"];
writable - madonna
writable - tiesto
writable - atb
handle_write - atb
handle_write - atb
...................
handle_write - atb
这就是我要的:
writable - madonna
handle_write - madonna
writable - tiesto
handle_write - tiesto
writable - atb
handle_write - atb