def wait(self, method, callback=None, timeout=None, returns_tuple=False): p = ensure_promise(callback) pending = self._pending prev_p = [] if not isinstance(method, list): method = [method] for m in method: prev_p.append(pending.get(m)) pending[m] = p try: while not p.ready: self.connection.drain_events(timeout=timeout) if p.value: args, kwargs = p.value args = args[1:] # We are not returning method back return args if returns_tuple else (args and args[0]) finally: for i, m in enumerate(method): if prev_p[i] is not None: pending[m] = prev_p[i] else: pending.pop(m, None)
def wait(self, method, callback=None, timeout=None, returns_tuple=False): p = ensure_promise(callback) pending = self._pending prev_p = [] if not isinstance(method, list): method = [method] for m in method: prev_p.append(pending.get(m)) pending[m] = p try: while not p.ready: if self.connection: self.connection.drain_events(timeout=timeout) if p.value: args, kwargs = p.value args = args[1:] # We are not returning method back return args if returns_tuple else (args and args[0]) finally: for i, m in enumerate(method): if prev_p[i] is not None: pending[m] = prev_p[i] else: pending.pop(m, None)
def _get_bulk_async(self, queue, max_if_unlimited=SQS_MAX_MESSAGES, callback=None): maxcount = self._get_message_estimate() if maxcount: return self._get_async(queue, maxcount, callback=callback) # Not allowed to consume, make sure to notify callback.. callback = ensure_promise(callback) callback([]) return callback
def __init__(self, host='localhost:5672', userid='guest', password='******', login_method=None, login_response=None, authentication=(), virtual_host='/', locale='en_US', client_properties=None, ssl=False, connect_timeout=None, channel_max=None, frame_max=None, heartbeat=0, on_open=None, on_blocked=None, on_unblocked=None, confirm_publish=False, on_tune_ok=None, read_timeout=None, write_timeout=None, socket_settings=None, frame_handler=frame_handler, frame_writer=frame_writer, **kwargs): self._connection_id = uuid.uuid4().hex channel_max = channel_max or 65535 frame_max = frame_max or 131072 if authentication: if isinstance(authentication, sasl.SASL): authentication = (authentication, ) self.authentication = authentication elif login_method is not None: if login_method == 'GSSAPI': auth = sasl.GSSAPI(userid) elif login_method == 'EXTERNAL': auth = sasl.EXTERNAL() elif login_method == 'AMQPLAIN': if userid is None or password is None: raise ValueError( "Must supply authentication or userid/password") auth = sasl.AMQPLAIN(userid, password) elif login_method == 'PLAIN': if userid is None or password is None: raise ValueError( "Must supply authentication or userid/password") auth = sasl.PLAIN(userid, password) elif login_response is not None: auth = sasl.RAW(login_method, login_response) else: raise ValueError("Invalid login method", login_method) self.authentication = (auth, ) else: self.authentication = (sasl.GSSAPI(userid, fail_soft=True), sasl.EXTERNAL(), sasl.AMQPLAIN(userid, password), sasl.PLAIN(userid, password)) self.client_properties = dict(self.library_properties, **client_properties or {}) self.locale = locale self.host = host self.virtual_host = virtual_host self.on_tune_ok = ensure_promise(on_tune_ok) self.frame_handler_cls = frame_handler self.frame_writer_cls = frame_writer self._handshake_complete = False self.channels = {} # The connection object itself is treated as channel 0 super(Connection, self).__init__(self, 0) self._frame_writer = None self._on_inbound_frame = None self._transport = None # Properties set in the Tune method self.channel_max = channel_max self.frame_max = frame_max self.client_heartbeat = heartbeat self.confirm_publish = confirm_publish self.ssl = ssl self.read_timeout = read_timeout self.write_timeout = write_timeout self.socket_settings = socket_settings # Callbacks self.on_blocked = on_blocked self.on_unblocked = on_unblocked self.on_open = ensure_promise(on_open) self._avail_channel_ids = array('H', range(self.channel_max, 0, -1)) # Properties set in the Start method self.version_major = 0 self.version_minor = 0 self.server_properties = {} self.mechanisms = [] self.locales = [] self.connect_timeout = connect_timeout
def __init__(self, host='localhost:5672', userid='guest', password='******', login_method='AMQPLAIN', login_response=None, virtual_host='/', locale='en_US', client_properties=None, ssl=False, connect_timeout=None, channel_max=None, frame_max=None, heartbeat=0, on_open=None, on_blocked=None, on_unblocked=None, confirm_publish=False, on_tune_ok=None, read_timeout=None, write_timeout=None, socket_settings=None, frame_handler=frame_handler, frame_writer=frame_writer, **kwargs): """Create a connection to the specified host, which should be a 'host[:port]', such as 'localhost', or '1.2.3.4:5672' (defaults to 'localhost', if a port is not specified then 5672 is used) If login_response is not specified, one is built up for you from userid and password if they are present. The 'ssl' parameter may be simply True/False, or for Python >= 2.6 a dictionary of options to pass to ssl.wrap_socket() such as requiring certain certificates. The "socket_settings" parameter is a dictionary defining tcp settings which will be applied as socket options. """ self._connection_id = uuid.uuid4().hex channel_max = channel_max or 65535 frame_max = frame_max or 131072 if (login_response is None) \ and (userid is not None) \ and (password is not None): login_response = BytesIO() _write_table({ 'LOGIN': userid, 'PASSWORD': password }, login_response.write, []) # Skip the length at the beginning login_response = login_response.getvalue()[4:] self.client_properties = dict(LIBRARY_PROPERTIES, **client_properties or {}) self.login_method = login_method self.login_response = login_response self.locale = locale self.host = host self.virtual_host = virtual_host self.on_tune_ok = ensure_promise(on_tune_ok) self.frame_handler_cls = frame_handler self.frame_writer_cls = frame_writer self._handshake_complete = False self.channels = {} # The connection object itself is treated as channel 0 super(Connection, self).__init__(self, 0) self._frame_writer = None self._on_inbound_frame = None self._transport = None # Properties set in the Tune method self.channel_max = channel_max self.frame_max = frame_max self.client_heartbeat = heartbeat self.confirm_publish = confirm_publish self.ssl = ssl self.read_timeout = read_timeout self.write_timeout = write_timeout self.socket_settings = socket_settings # Callbacks self.on_blocked = on_blocked self.on_unblocked = on_unblocked self.on_open = ensure_promise(on_open) self._avail_channel_ids = array('H', range(self.channel_max, 0, -1)) # Properties set in the Start method self.version_major = 0 self.version_minor = 0 self.server_properties = {} self.mechanisms = [] self.locales = [] self.connect_timeout = connect_timeout
def __init__(self, host='localhost:5672', userid='guest', password='******', login_method=None, login_response=None, authentication=(), virtual_host='/', locale='en_US', client_properties=None, ssl=False, connect_timeout=None, channel_max=None, frame_max=None, heartbeat=0, on_open=None, on_blocked=None, on_unblocked=None, confirm_publish=False, on_tune_ok=None, read_timeout=None, write_timeout=None, socket_settings=None, frame_handler=frame_handler, frame_writer=frame_writer, **kwargs): self._connection_id = uuid.uuid4().hex channel_max = channel_max or 65535 frame_max = frame_max or 131072 if authentication: if isinstance(authentication, sasl.SASL): authentication = (authentication,) self.authentication = authentication elif login_method is not None: if login_method == 'GSSAPI': auth = sasl.GSSAPI(userid) elif login_method == 'EXTERNAL': auth = sasl.EXTERNAL() elif login_method == 'AMQPLAIN': if userid is None or password is None: raise ValueError( "Must supply authentication or userid/password") auth = sasl.AMQPLAIN(userid, password) elif login_method == 'PLAIN': if userid is None or password is None: raise ValueError( "Must supply authentication or userid/password") auth = sasl.PLAIN(userid, password) elif login_response is not None: auth = sasl.RAW(login_method, login_response) else: raise ValueError("Invalid login method", login_method) self.authentication = (auth,) else: self.authentication = (sasl.GSSAPI(userid, fail_soft=True), sasl.EXTERNAL(), sasl.AMQPLAIN(userid, password), sasl.PLAIN(userid, password)) self.client_properties = dict( self.library_properties, **client_properties or {} ) self.locale = locale self.host = host self.virtual_host = virtual_host self.on_tune_ok = ensure_promise(on_tune_ok) self.frame_handler_cls = frame_handler self.frame_writer_cls = frame_writer self._handshake_complete = False self.channels = {} # The connection object itself is treated as channel 0 super(Connection, self).__init__(self, 0) self._frame_writer = None self._on_inbound_frame = None self._transport = None # Properties set in the Tune method self.channel_max = channel_max self.frame_max = frame_max self.client_heartbeat = heartbeat self.confirm_publish = confirm_publish self.ssl = ssl self.read_timeout = read_timeout self.write_timeout = write_timeout self.socket_settings = socket_settings # Callbacks self.on_blocked = on_blocked self.on_unblocked = on_unblocked self.on_open = ensure_promise(on_open) self._avail_channel_ids = array.array('H', range(self.channel_max, 0, -1)) # Properties set in the Start method self.version_major = 0 self.version_minor = 0 self.server_properties = {} self.mechanisms = [] self.locales = [] self.connect_timeout = connect_timeout
def __init__(self, host='localhost:5672', userid='guest', password='******', login_method='AMQPLAIN', login_response=None, virtual_host='/', locale='en_US', client_properties=None, ssl=False, connect_timeout=None, channel_max=None, frame_max=None, heartbeat=0, on_open=None, on_blocked=None, on_unblocked=None, confirm_publish=False, on_tune_ok=None, read_timeout=None, write_timeout=None, socket_settings=None, frame_handler=frame_handler, frame_writer=frame_writer, **kwargs): """Create a connection to the specified host, which should be a 'host[:port]', such as 'localhost', or '1.2.3.4:5672' (defaults to 'localhost', if a port is not specified then 5672 is used) If login_response is not specified, one is built up for you from userid and password if they are present. The 'ssl' parameter may be simply True/False, or for Python >= 2.6 a dictionary of options to pass to ssl.wrap_socket() such as requiring certain certificates. The "socket_settings" parameter is a dictionary defining tcp settings which will be applied as socket options. """ self._connection_id = uuid.uuid4().hex channel_max = channel_max or 65535 frame_max = frame_max or 131072 if (login_response is None) \ and (userid is not None) \ and (password is not None): login_response = BytesIO() _write_table({'LOGIN': userid, 'PASSWORD': password}, login_response.write, []) # Skip the length at the beginning login_response = login_response.getvalue()[4:] self.client_properties = dict( self.library_properties, **client_properties or {} ) self.login_method = login_method self.login_response = login_response self.locale = locale self.host = host self.virtual_host = virtual_host self.on_tune_ok = ensure_promise(on_tune_ok) self.frame_handler_cls = frame_handler self.frame_writer_cls = frame_writer self._handshake_complete = False self.channels = {} # The connection object itself is treated as channel 0 super(Connection, self).__init__(self, 0) self._frame_writer = None self._on_inbound_frame = None self._transport = None # Properties set in the Tune method self.channel_max = channel_max self.frame_max = frame_max self.client_heartbeat = heartbeat self.confirm_publish = confirm_publish self.ssl = ssl self.read_timeout = read_timeout self.write_timeout = write_timeout self.socket_settings = socket_settings # Callbacks self.on_blocked = on_blocked self.on_unblocked = on_unblocked self.on_open = ensure_promise(on_open) self._avail_channel_ids = array('H', range(self.channel_max, 0, -1)) # Properties set in the Start method self.version_major = 0 self.version_minor = 0 self.server_properties = {} self.mechanisms = [] self.locales = [] self.connect_timeout = connect_timeout
def __init__(self, host='localhost:5672', userid='guest', password='******', login_method='AMQPLAIN', login_response=None, virtual_host='/', locale='en_US', client_properties=None, ssl=False, connect_timeout=None, channel_max=None, frame_max=None, heartbeat=0, on_open=None, on_blocked=None, on_unblocked=None, confirm_publish=False, on_tune_ok=None, read_timeout=None, write_timeout=None, socket_settings=None, frame_handler=frame_handler, frame_writer=frame_writer, **kwargs): self._connection_id = uuid.uuid4().hex channel_max = channel_max or 65535 frame_max = frame_max or 131072 if (login_response is None) \ and (userid is not None) \ and (password is not None): login_response = BytesIO() _write_table({ 'LOGIN': userid, 'PASSWORD': password }, login_response.write, []) # Skip the length at the beginning login_response = login_response.getvalue()[4:] self.client_properties = dict(self.library_properties, **client_properties or {}) self.login_method = login_method self.login_response = login_response self.locale = locale self.host = host self.virtual_host = virtual_host self.on_tune_ok = ensure_promise(on_tune_ok) self.frame_handler_cls = frame_handler self.frame_writer_cls = frame_writer self._handshake_complete = False self.channels = {} # The connection object itself is treated as channel 0 super(Connection, self).__init__(self, 0) self._frame_writer = None self._on_inbound_frame = None self._transport = None # Properties set in the Tune method self.channel_max = channel_max self.frame_max = frame_max self.client_heartbeat = heartbeat self.confirm_publish = confirm_publish self.ssl = ssl self.read_timeout = read_timeout self.write_timeout = write_timeout self.socket_settings = socket_settings # Callbacks self.on_blocked = on_blocked self.on_unblocked = on_unblocked self.on_open = ensure_promise(on_open) self._avail_channel_ids = array('H', range(self.channel_max, 0, -1)) # Properties set in the Start method self.version_major = 0 self.version_minor = 0 self.server_properties = {} self.mechanisms = [] self.locales = [] self.connect_timeout = connect_timeout
def __init__(self, host='localhost:5672', userid='guest', password='******', login_method='AMQPLAIN', login_response=None, virtual_host='/', locale='en_US', client_properties=None, ssl=False, connect_timeout=None, channel_max=None, frame_max=None, heartbeat=0, on_open=None, on_blocked=None, on_unblocked=None, confirm_publish=False, on_tune_ok=None, read_timeout=None, write_timeout=None, socket_settings=None, frame_handler=frame_handler, frame_writer=frame_writer, **kwargs): self._connection_id = uuid.uuid4().hex channel_max = channel_max or 65535 frame_max = frame_max or 131072 if (login_response is None) \ and (userid is not None) \ and (password is not None): login_response = BytesIO() _write_table({'LOGIN': userid, 'PASSWORD': password}, login_response.write, []) # Skip the length at the beginning login_response = login_response.getvalue()[4:] self.client_properties = dict( self.library_properties, **client_properties or {} ) self.login_method = login_method self.login_response = login_response self.locale = locale self.host = host self.virtual_host = virtual_host self.on_tune_ok = ensure_promise(on_tune_ok) self.frame_handler_cls = frame_handler self.frame_writer_cls = frame_writer self._handshake_complete = False self.channels = {} # The connection object itself is treated as channel 0 super(Connection, self).__init__(self, 0) self._frame_writer = None self._on_inbound_frame = None self._transport = None # Properties set in the Tune method self.channel_max = channel_max self.frame_max = frame_max self.client_heartbeat = heartbeat self.confirm_publish = confirm_publish self.ssl = ssl self.read_timeout = read_timeout self.write_timeout = write_timeout self.socket_settings = socket_settings # Callbacks self.on_blocked = on_blocked self.on_unblocked = on_unblocked self.on_open = ensure_promise(on_open) self._avail_channel_ids = array('H', range(self.channel_max, 0, -1)) # Properties set in the Start method self.version_major = 0 self.version_minor = 0 self.server_properties = {} self.mechanisms = [] self.locales = [] self.connect_timeout = connect_timeout