コード例 #1
0
    def __init__(self, connection_params_dict, queues_to_declare=(), **kwargs):

        super(BaseAMQPTool, self).__init__(**kwargs)

        # resolve the `connection_params_dict` argument if specified as None
        if connection_params_dict is None:
            connection_params_dict = get_amqp_connection_params_dict()
        if connection_params_dict.get('ssl'):
            connection_params_dict.setdefault(
                'credentials', pika.credentials.ExternalCredentials())
        if 'client_properties' not in connection_params_dict:
            connection_params_dict['client_properties'] = \
                get_n6_default_client_properties_for_amqp_connection()

        # normalize the `queues_to_declare` argument
        # -- to a list of kwargs for queue_declare()
        if is_seq(queues_to_declare):
            queues_to_declare = list(queues_to_declare)
        else:
            queues_to_declare = [queues_to_declare]
        for i, queue in enumerate(queues_to_declare):
            queue = queues_to_declare[i] = (dict(queue) if isinstance(
                queue, collections_abc.Mapping) else {
                    'queue': queue
                })
            queue.setdefault('callback', (lambda *args, **kwargs: None))

        # set several non-public instance attributes
        self._connection_params_dict = connection_params_dict
        self._queues_to_declare = queues_to_declare

        self._shutdown_lock = threading.Lock()

        self._connection_lock = threading.Lock()
        self._connection_lock_nonblocking = NonBlockingLockWrapper(
            self._connection_lock,
            lock_description='the connection/channel operations lock')
        self._connection_closed = False

        # setup AMQP communication
        self._setup_communication()
コード例 #2
0
    def __init__(self, queue_bindings, exchanges_to_declare=(), **kwargs):
        """
        Initialize the instance (opening AMQP connection etc.).

        Obligatory kwargs:
            `connection_params_dict` (dict or None; obligatory):
                A dict to be passed as **kwargs into the
                pika.ConnectionParameters constructor.  It can be
                explicitly specified as None -- then it will be obtained
                automatically using the function
                amqp_helpers.get_amqp_connection_params_dict().

            `queue_bindings` (dict or sequence of dicts; obligatory):
                A dict to be passed as **kwargs into
                pika.channel.Channel.queue_bind(), or a sequence
                of such dicts.  Dict(s) do not need to contain
                the 'callback' item (by default a no-op callable
                is used).

        Optional kwargs:
            `queues_to_declare` (str/dict/sequence...; default: empty tuple):
                The name of a queue to be declared or a dict of **kwargs
                for pika.channel.Channel.queue_declare(), or a sequence
                of such names or dicts.  Dict(s) do not need to contain
                the 'callback' item (by default a no-op callable is used).

            `exchanges_to_declare` (str/dict/seq...; default: empty tuple):
                The name of an exchange to be declared or a dict of **kwargs
                for pika.channel.Channel.exchange_declare(), or a sequence
                of such names or dicts.

        Raises:
            A pika.exceptions.AMQPError subclass:
                If AMQP connection cannot be set up.
        """

        # normalize the `queue_bindings` argument
        # -- to a list of kwargs for queue_bind()
        if is_seq(queue_bindings):
            queue_bindings = list(queue_bindings)
        else:
            queue_bindings = [queue_bindings]
        for i, bind_kwargs in enumerate(queue_bindings):
            bind_kwargs = queue_bindings[i] = dict(bind_kwargs)
            bind_kwargs.setdefault('callback', (lambda *args, **kwargs: None))

        # normalize the `exchanges_to_declare` argument
        # -- to a list of kwargs for exchange_declare()
        if is_seq(exchanges_to_declare):
            exchanges_to_declare = list(exchanges_to_declare)
        else:
            exchanges_to_declare = [exchanges_to_declare]
        for i, exchange in enumerate(exchanges_to_declare):
            exchanges_to_declare[i] = (dict(exchange) if isinstance(
                exchange, collections_abc.Mapping) else {
                    'exchange': exchange
                })

        self._exchanges_to_declare = exchanges_to_declare
        self._queue_bindings = queue_bindings

        super(AMQPSimpleGetter, self).__init__(**kwargs)
コード例 #3
0
def make_multiadjuster(self, value, singular_adjuster=None):
    if not is_seq(value):
        value = (value, )
    if singular_adjuster is None:
        return list(value)
    return [singular_adjuster(self, el) for el in value]