Commits

Reid Draper  committed f228cef

this is ugly, but seems to work using yield

  • Participants
  • Parent commits 26e1551

Comments (0)

Files changed (1)

 import socket
 from collections import deque
 
-from tornado import iostream
+from tornado import iostream, ioloop
 import swirl
 
 class RedisTornado(object):
         self.port       = port or 6379
         self._queue     = deque()
         self.callback   = None
+        self._waiting   = False
         self.DELIMITER  = '\r\n'
         self.DELBYTES   = len(self.DELIMITER)
         
         
         self.stream = iostream.IOStream(sock)
         
-    def _is_waiting(self):
-        return self.stream.reading() or self.stream.writing()
         
     @swirl.asynchronous    
     def _work(self):
         print 'work, yeah'
-        while True:
-            try:
-                cmd, callback = self._queue.popleft()
-            except IndexError:
-                print 'no more to pop'
+        self._waiting = True
+        try:
+            cmd, callback = self._queue.popleft()
+        except IndexError:
+            print 'no more to pop'
+            self._waiting = False
+            return
+            
+        yield lambda cb : self.stream.write(cmd, cb)
+        response = yield lambda cb : self.stream.read_until(self.DELIMITER, cb)
+        print 'the response is %s' % response
+        
+        if response[0] == '$':
+            print 'the response had a $'
+            num_bytes = int( response[1:] )
+            if num_bytes < 1:
+                callback(None)
+                self.waiting = False
+                # ioloop.IOLoop.instance().add_callback(_work)
+                self._work()
                 return
-                
-            yield lambda cb : self.stream.write(cmd, cb)
-            response = yield lambda cb : self.stream.read_until(self.DELIMITER, cb)
-            print 'the response is %s' % response
+            print 'the number of bytes was %d' % num_bytes
+            response = yield lambda cb : self.stream.read_bytes(num_bytes + self.DELBYTES, cb)
+            print 'the new response is %s' % response
+            callback(response)
+            self._waiting = False
+            # ioloop.IOLoop.instance().add_callback(_work)
+            self._work()
+            return
             
-            if response[0] == '$':
-                print 'the response had a $'
-                num_bytes = int( response[1:] )
-                if num_bytes < 1:
-                    callback(None)
-                    return
-                print 'the number of bytes was %d' % num_bytes
-                response = yield lambda cb : self.stream.read_bytes(num_bytes + self.DELBYTES, cb)
-                print 'the new response is %s' % response
-                callback(response)
-                return
-                
-            elif response[0] == '+':
-                print 'confirmation'
-                callback(True)
-                return
-            
-            elif response[0] == '-':
-                callback(False)
-                return
+        elif response[0] == '+':
+            print 'confirmation'
+            callback(True)
+            self._waiting = False
+            # ioloop.IOLoop.instance().add_callback(_work)
+            self._work()
+            return
+        
+        elif response[0] == '-':
+            callback(False)
+            self._waiting = False
+            # ioloop.IOLoop.instance().add_callback(_work)
+            self._work()
+            return
         
     def set(self, key, value, callback = None):
         cmd = '%s %s %s\r\n%s\r\n' % ('SET', key, len(value), value)
         self._queue.append( (cmd, callback) )
         print 'added to the queue'
-        if not self._is_waiting():
+        if not self._waiting:
             self._work()
         
     def get(self, key, callback = None):
         cmd = 'GET %s\r\n' % key
         self._queue.append( (cmd, callback) )
         print 'added to the queue'
-        if not self._is_waiting():
+        if not self._waiting:
             self._work()