Python's asyncore client send only last element from list inside a thread -


this 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 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):         = 0         while self._stop == false , < len(artists.artistlist):             self.client.sendartist(artists.artistlist[i])             += 1  client = testclient("127.0.0.1", 7899) asyncore.loop() 

my problem inside run method in sender class, each item in artist.artistlist when sendartist() called, writable() called of them , handle_write() last item.

what can handle_write() called each item in list , not last 1 ?

this how working now: artists.artistlist = ["madonna", "tiesto", "atb"];

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

this want:

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

asyncore asynchronous framework, such, don't control when write network.

you have 2 options here:

  • use regular, synchronous sockets
  • append buffer instead of replacing it

the second option quite self-explanatory, here's how you'd 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() 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -