Last active
March 9, 2018 17:29
-
-
Save mattbennett/888e42af88c2733918ad1810956abbde to your computer and use it in GitHub Desktop.
Kombu unsafe default socket timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kombu import Connection | |
import time | |
import threading | |
import socket | |
AMQP_URI = "pyamqp://guest:guest@localhost:5672/" | |
DEFAULT_TIMEOUT = 1.0 | |
run = threading.Event() | |
socket.setdefaulttimeout(DEFAULT_TIMEOUT) | |
def one(): | |
while run.is_set(): | |
with Connection(AMQP_URI) as conn: | |
conn.collect(socket_timeout=0.1) | |
def two(): | |
while run.is_set(): | |
try: | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
assert sock.gettimeout() == DEFAULT_TIMEOUT | |
except AssertionError: | |
run.clear() | |
raise | |
run.set() | |
t1 = threading.Thread(target=one) | |
t2 = threading.Thread(target=two) | |
try: | |
t1.start() | |
t2.start() | |
while run.is_set(): | |
time.sleep(1) | |
except KeyboardInterrupt: | |
run.clear() | |
t1.join() | |
t2.join() |
Author
mattbennett
commented
Mar 9, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment