Package: taskiq-aio-pika
AioPika broker for taskiqThis lirary provides you with aio-pika broker for taskiq.Usage:fromtaskiq_aio_pikaimportAioPikaBrokerbroker=AioPikaBroker()@broker.taskasyncdeftest()->None:print("nothing")Non-obvious thingsYou can send delayed messages and set priorities to messages using labels.DelaysDefault retriesTo send delayed message, you have to specify
delay label. You can do it withtaskdecorator,
or by using kicker.In this type of delay we are using additional queue withexpirationparameter and after with time message will be deleted fromdelayqueue and sent to the main taskiq queue.
For example:broker=AioPikaBroker()@broker.task(delay=3)asyncdefdelayed_task()->int:return1asyncdefmain():awaitbroker.startup()# This message will be received by workers# After 3 seconds delay.awaitdelayed_task.kiq()# This message is going to be received after the delay in 4 seconds.# Since we overriden the `delay` label using kicker.awaitdelayed_task.kicker().with_labels(delay=4).kiq()# This message is going to be send immediately. Since we deleted the label.awaitdelayed_task.kicker().with_labels(delay=None).kiq()# Of course the delay is managed by rabbitmq, so you don't# have to wait delay period before message is going to be sent.Retries withrabbitmq-delayed-message-exchangepluginTo send delayed message you can installrabbitmq-delayed-message-exchangepluginhttps://github.com/rabbitmq/rabbitmq-delayed-message-exchange.And you need to configure you broker.
There isdelayed_message_exchange_pluginAioPikaBrokerparameter and it must beTrueto turn on delayed message functionality.The delay plugin can handle tasks with different delay times well, and the delay based on dead letter queue is suitable for tasks with the same delay time.For example:broker=AioPikaBroker(delayed_message_exchange_plugin=True,)@broker.task(delay=3)asyncdefdelayed_task()->int:return1asyncdefmain():awaitbroker.startup()# This message will be received by workers# After 3 seconds delay.awaitdelayed_task.kiq()# This message is going to be received after the delay in 4 seconds.# Since we overriden the `delay` label using kicker.awaitdelayed_task.kicker().with_labels(delay=4).kiq()PrioritiesYou can define priorities for messages usingprioritylabel.
Messages with higher priorities are delivered faster.
But to use priorities you need to definemax_priorityof the main queue, by passingmax_priorityparameter in broker's init.
This parameter sets maximum priority for the queue and
declares it as the prority queue.Before doing so please read thedocumentationabout what
downsides you get by using prioritized queues.broker=AioPikaBroker(max_priority=10)# We can define default priority for tasks.@broker.task(priority=2)asyncdefprio_task()->int:return1asyncdefmain():awaitbroker.startup()# This message has priority = 2.awaitprio_task.kiq()# This message is going to have priority 4.awaitprio_task.kicker().with_labels(priority=4).kiq()# This message is going to have priority 0.awaitprio_task.kicker().with_labels(priority=None).kiq()ConfigurationAioPikaBroker parameters:url- url to rabbitmq. If None, "amqp://guest:guest@localhost:5672" is used.result_backend- custom result backend.task_id_generator- custom task_id genertaor.exchange_name- name of exchange that used to send messages.exchange_type- type of the exchange. Used only ifdeclare_exchangeis True.queue_name- queue that used to get incoming messages.routing_key- that used to bind that queue to the exchange.declare_exchange- whether you want to declare new exchange if it doesn't exist.max_priority- maximum priority for messages.delay_queue_name- custom delay queue name.
This queue is used to deliver messages with delays.dead_letter_queue_name- custom dead letter queue name.
This queue is used to receive negatively acknowleged messages from the main queue.qos- number of messages that worker can prefetch.declare_queues- whether you want to declare queues even on
client side. May be useful for message persistance.