@@ -559,6 +559,7 @@ def __init__(
559
559
self .set_parser (parser_class )
560
560
self ._connect_callbacks = []
561
561
self ._buffer_cutoff = 6000
562
+ self ._expected_responses = 0
562
563
563
564
def __repr__ (self ):
564
565
repr_args = "," .join ([f"{ k } ={ v } " for k , v in self .repr_pieces ()])
@@ -739,6 +740,7 @@ def disconnect(self, *args):
739
740
except OSError :
740
741
pass
741
742
self ._sock = None
743
+ self ._expected_responses = 0
742
744
743
745
def _send_ping (self ):
744
746
"""Send PING, expect PONG in return"""
@@ -762,11 +764,17 @@ def send_packed_command(self, command, check_health=True):
762
764
# guard against health check recursion
763
765
if check_health :
764
766
self .check_health ()
767
+
768
+ # flush un-retrieved responses to any previous commands
769
+ while self ._expected_responses :
770
+ _ = self .read_response (disable_decoding = True )
771
+
765
772
try :
766
773
if isinstance (command , str ):
767
774
command = [command ]
768
775
for item in command :
769
776
self ._sock .sendall (item )
777
+ self ._expected_responses += 1
770
778
except socket .timeout :
771
779
self .disconnect ()
772
780
raise TimeoutError ("Timeout writing to socket" )
@@ -820,6 +828,8 @@ def read_response(self, disable_decoding=False):
820
828
self .disconnect ()
821
829
raise
822
830
831
+ self ._expected_responses -= 1
832
+
823
833
if self .health_check_interval :
824
834
self .next_health_check = time () + self .health_check_interval
825
835
0 commit comments