Skip to content

Instantly share code, notes, and snippets.

@minrk
Created May 5, 2011 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/958212 to your computer and use it in GitHub Desktop.
Save minrk/958212 to your computer and use it in GitHub Desktop.
import socket
def flush(s, buf=1024):
"""recv the most recent message, ignoring queued values"""
# save timeout
to = s.gettimeout()
# recv first message in blocking manner
msg = s.recv(buf)
s.settimeout(0)
while True:
try:
msg = s.recv(buf)
except socket.error:
# stop recving when there's nothing to recv
break
# restore timeout
s.settimeout(to)
return msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment