From a69b76571943d1ceebf7cf2d5f393c4cbab76d8e Mon Sep 17 00:00:00 2001 From: David Koblas Date: Sun, 28 Jun 2015 12:27:04 -0700 Subject: [PATCH] Queue commands on a client such that it's not possible to have multiple commands being handled and parsed. --- tornadoredis/client.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tornadoredis/client.py b/tornadoredis/client.py index 0f7fb7e..faee49a 100644 --- a/tornadoredis/client.py +++ b/tornadoredis/client.py @@ -244,6 +244,8 @@ def __init__(self, host='localhost', port=6379, unix_socket_path=None, self.password = password self.selected_db = selected_db or 0 self._pipeline = None + self.executing = False + self.command_queue = deque() def __del__(self): try: @@ -385,11 +387,20 @@ def format_reply(self, cmd_line, data): return res #### + @gen.engine + def queue_command(self, callback=None): + self.command_queue.append(callback) + @gen.engine def execute_command(self, cmd, *args, **kwargs): result = None execute_pending = cmd not in ('AUTH', 'SELECT') + # You can only one run command down a connection at a time, otherwise you end up with parsing issues + if self.command_queue: + yield gen.Task(self.queue_command) + self.command_queue.appendleft(None) + callback = kwargs.get('callback', None) if 'callback' in kwargs: del kwargs['callback'] @@ -447,6 +458,13 @@ def execute_command(self, cmd, *args, **kwargs): if execute_pending: self.connection.execute_pending_command() + # Dequeue any pending commands + while self.command_queue: + cb = self.command_queue.popleft() + if cb is not None: + cb() + break + if callback: callback(result)