Frage

This is my client:

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()

My problem is inside the run method in Sender class, for each item in artist.artistList when sendArtist() is called, writable() will be called for all of them and handle_write() only for the last item.

What can I do so that handle_write() will be called for each item in the list and not only for the last one ?

This is how is working now: artists.artistList = ["madonna", "tiesto", "atb"];

writable - madonna
writable - tiesto
writable - atb
handle_write - atb
handle_write - atb
...................
handle_write - atb

This is what I want:

writable - madonna
handle_write - madonna
writable - tiesto
handle_write - tiesto
writable - atb
handle_write - atb
War es hilfreich?

Lösung

Asyncore is an asynchronous framework, as such, you don't control when you write to the network.

You have two options here:

  • Use regular, synchronous sockets
  • Append to the buffer instead of replacing it

The second option is quite self-explanatory, so here's how you'd do it with regular sockets:

import socket

class TestClient(object):
    _buffer = ""

    def __init__(self, host, port):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))

    def writable(self):
        return (len(self._buffer) > 0)

    def write(self):
        while self.writable():
            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)
        self.write()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top