-
Notifications
You must be signed in to change notification settings - Fork 15
feat: multiqueue implementation, support for custom settings for queues/exchange #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danfimov
wants to merge
2
commits into
master
Choose a base branch
from
multique-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| .DEFAULT: | ||
| @echo "No such command (or you pass two or many targets to ). List of possible commands: make help" | ||
|
|
||
| .DEFAULT_GOAL := help | ||
|
|
||
| ##@ Local development | ||
|
|
||
| .PHONY: help | ||
| help: ## Show this help | ||
| @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target> <arg=value>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m %s\033[0m\n\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
|
||
| .PHONY: clear_rabbit | ||
| clear_rabbit: ## Clear RabbitMQ data volume and restart container | ||
| @docker stop taskiq_aio_pika_rabbitmq && docker rm taskiq_aio_pika_rabbitmq && docker volume rm taskiq-aio-pika_rabbitmq_data && docker compose up -d | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """ | ||
| Basic example of using Taskiq with AioPika broker. | ||
|
|
||
| How to run: | ||
| 1. Run worker: taskiq worker examples.basic:broker -w 1 | ||
| 2. Run broker: uv run examples/basic.py | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| from taskiq_redis import RedisAsyncResultBackend | ||
|
|
||
| from taskiq_aio_pika import AioPikaBroker | ||
|
|
||
| broker = AioPikaBroker( | ||
| "amqp://guest:guest@localhost:5672/", | ||
| ).with_result_backend(RedisAsyncResultBackend("redis://localhost:6379/0")) | ||
|
|
||
|
|
||
| @broker.task | ||
| async def add_one(value: int) -> int: | ||
| return value + 1 | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| await broker.startup() | ||
| # Send the task to the broker. | ||
| task = await add_one.kiq(1) | ||
| # Wait for the result. | ||
| result = await task.wait_result(timeout=2) | ||
| print(f"Task execution took: {result.execution_time} seconds.") | ||
| if not result.is_err: | ||
| print(f"Returned value: {result.return_value}") | ||
| else: | ||
| print("Error found while executing task.") | ||
| await broker.shutdown() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """ | ||
| Example of delayed task execution using Taskiq with AioPika broker. | ||
|
|
||
| How to run: | ||
| 1. Run worker: taskiq worker examples.delayed_task:broker -w 1 | ||
| 2. Run broker: uv run examples/delayed_task.py | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| from taskiq_redis import RedisAsyncResultBackend | ||
|
|
||
| from taskiq_aio_pika import AioPikaBroker | ||
|
|
||
| broker = AioPikaBroker( | ||
| "amqp://guest:guest@localhost:5672/", | ||
| ).with_result_backend(RedisAsyncResultBackend("redis://localhost:6379/0")) | ||
|
|
||
|
|
||
| @broker.task | ||
| async def add_one(value: int) -> int: | ||
| return value + 1 | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| await broker.startup() | ||
| # Send the task to the broker. | ||
| task = await add_one.kicker().with_labels(delay=2).kiq(1) | ||
| print("Task sent with 2 seconds delay.") | ||
| # Wait for the result. | ||
| result = await task.wait_result(timeout=3) | ||
| print(f"Task execution took: {result.execution_time} seconds.") | ||
| if not result.is_err: | ||
| print(f"Returned value: {result.return_value}") | ||
| else: | ||
| print("Error found while executing task.") | ||
| await broker.shutdown() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """ | ||
| Example with two queues for different workers and one topic exchange. | ||
|
|
||
| It can be useful when you want to have two worker | ||
danfimov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| How to run: | ||
| 1. Run worker for queue_1: taskiq worker examples.topic_with_two_queues:get_broker_for_queue_1 -w 1 | ||
| 2. Run worker for queue_2: taskiq worker examples.topic_with_two_queues:get_broker_for_queue_2 -w 1 | ||
| 3. Run broker to send a task: uv run examples/topic_with_two_queues.py --queue 1 | ||
| 4. Optionally run broker to send a task to other queue: uv run examples/topic_with_two_queues.py --queue 2 | ||
| """ | ||
|
|
||
| import argparse | ||
| import asyncio | ||
| import uuid | ||
|
|
||
| from aio_pika.abc import ExchangeType | ||
| from taskiq_redis import RedisAsyncResultBackend | ||
|
|
||
| from taskiq_aio_pika import AioPikaBroker, Exchange, Queue, QueueType | ||
|
|
||
| broker = AioPikaBroker( | ||
| "amqp://guest:guest@localhost:5672/", | ||
| exchange=Exchange( | ||
| name="topic_exchange", | ||
| type=ExchangeType.TOPIC, | ||
| ), | ||
| delay_queue=Queue( | ||
| name="taskiq.delay", | ||
| routing_key="queue1", | ||
| ), # send delayed messages to queue1 | ||
| ).with_result_backend(RedisAsyncResultBackend("redis://localhost:6379/0")) | ||
|
|
||
|
|
||
| @broker.task | ||
| async def add_one(value: int) -> int: | ||
| return value + 1 | ||
|
|
||
|
|
||
| queue_1 = Queue( | ||
| name="queue1", | ||
| type=QueueType.CLASSIC, | ||
| durable=False, | ||
| ) | ||
| queue_2 = Queue( | ||
| name="queue2", | ||
| type=QueueType.CLASSIC, | ||
| durable=False, | ||
| ) | ||
|
|
||
|
|
||
| def get_broker_for_queue_1() -> AioPikaBroker: | ||
| print("This broker will listen to queue1") | ||
| return broker.with_queue(queue_1) | ||
|
|
||
|
|
||
| def get_broker_for_queue_2() -> AioPikaBroker: | ||
| print("This broker will listen to queue2") | ||
| return broker.with_queue(queue_2) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--queue", | ||
| choices=["1", "2"], | ||
| required=True, | ||
| help="Queue to send the task to.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| queue_name = queue_1.name if args.queue == "1" else queue_2.name | ||
|
|
||
| broker.with_queues( | ||
| queue_1, | ||
| queue_2, | ||
| ) # declare both queues to know about them during publishing | ||
| await broker.startup() | ||
|
|
||
| task = ( | ||
| await add_one.kicker() | ||
| .with_labels(queue_name=queue_name) # or it can be routing_key from queue_1 | ||
| .with_task_id(uuid.uuid4().hex) | ||
| .kiq(2) | ||
| ) | ||
| result = await task.wait_result(timeout=2) | ||
| print(f"Task execution took: {result.execution_time} seconds.") | ||
| if not result.is_err: | ||
| print(f"Returned value: {result.return_value}") | ||
| else: | ||
| print("Error found while executing task.") | ||
| await broker.shutdown() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.