Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Queue commands on a connection - fixes #83 #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tornadoredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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)

Expand Down