Esempio n. 1
0
    async def basic_consume(
        self,
        queue: str,
        consumer_callback: ConsumerCallback,
        *,
        no_ack: bool = False,
        exclusive: bool = False,
        arguments: ArgumentsType = None,
        consumer_tag: str = None
    ) -> spec.Basic.ConsumeOk:

        consumer_tag = consumer_tag or "ctag%i.%s" % (
            self.number,
            hexlify(os.urandom(16)).decode(),
        )

        if consumer_tag in self.consumers:
            raise exc.DuplicateConsumerTag(self.number)

        self.consumers[consumer_tag] = awaitable(consumer_callback)

        # noinspection PyTypeChecker
        return await self.rpc(
            spec.Basic.Consume(
                queue=queue,
                no_ack=no_ack,
                exclusive=exclusive,
                consumer_tag=consumer_tag,
                arguments=arguments,
            ),
        )
Esempio n. 2
0
    async def register(self, method_name, func: CallbackType, **kwargs):
        """ Method creates a queue with name which equal of
        `method_name` argument. Then subscribes this queue.

        :param method_name: Method name
        :param func:
            target function. Function **MUST** accept only keyword arguments.
        :param kwargs: arguments which will be passed to `queue_declare`
        :raises RuntimeError:
            Function already registered in this :class:`RPC` instance
            or method_name already used.
        """
        arguments = kwargs.pop("arguments", {})
        arguments.update({"x-dead-letter-exchange": self.DLX_NAME})

        kwargs["arguments"] = arguments

        queue = await self.channel.declare_queue(method_name, **kwargs)

        if func in self.consumer_tags:
            raise RuntimeError("Function already registered")

        if method_name in self.routes:
            raise RuntimeError(
                "Method name already used for %r" % self.routes[method_name],
            )

        self.consumer_tags[func] = await queue.consume(
            partial(self.on_call_message, method_name),
        )

        self.routes[method_name] = awaitable(func)
        self.queues[func] = queue
Esempio n. 3
0
    async def basic_consume(
            self,
            queue: str,
            consumer_callback: ConsumerCallback,
            *,
            no_ack: bool = False,
            exclusive: bool = False,
            arguments: ArgumentsType = None,
            consumer_tag: str = None,
            timeout: TimeoutType = None) -> spec.Basic.ConsumeOk:

        consumer_tag = consumer_tag or "ctag%i.%s" % (
            self.number,
            UUID(int=getrandbits(128), version=4).hex,
        )

        if consumer_tag in self.consumers:
            raise DuplicateConsumerTag(self.number)

        self.consumers[consumer_tag] = awaitable(consumer_callback)

        return await self.rpc(
            spec.Basic.Consume(
                queue=queue,
                no_ack=no_ack,
                exclusive=exclusive,
                consumer_tag=consumer_tag,
                arguments=arguments,
            ),
            timeout=timeout,
        )
Esempio n. 4
0
    async def create_worker(self, channel_name: str, func: Callable,
                            **kwargs) -> Worker:
        """ Creates a new :class:`Worker` instance. """

        queue = await self.create_queue(channel_name, **kwargs)

        if hasattr(func, "_is_coroutine"):
            fn = func
        else:
            fn = awaitable(func)
        consumer_tag = await queue.consume(partial(self.on_message, fn))

        return Worker(queue, consumer_tag, self.loop)
Esempio n. 5
0
 def __init__(self,
              constructor: ConstructorType,
              *args,
              max_size: int = None,
              loop: asyncio.AbstractEventLoop = None):
     self.loop = loop or asyncio.get_event_loop()
     self.__closed = False
     self.__constructor = awaitable(constructor)
     self.__constructor_args = args or ()
     self.__created = 0
     self.__item_set = set()
     self.__items = asyncio.Queue()
     self.__lock = asyncio.Lock()
     self.__max_size = max_size