-
-
Notifications
You must be signed in to change notification settings - Fork 124
experiment separate Broker + Router #624
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
Draft
GefMar
wants to merge
2
commits into
master
Choose a base branch
from
experiment/separate_broker
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.
Draft
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,69 @@ | ||
| """Route one task through several brokers with a shared router.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| from taskiq import Flow, InMemoryBroker, TaskiqRouter | ||
|
|
||
| router = TaskiqRouter() | ||
|
|
||
| default_email_flow = Flow("emails.default") | ||
| priority_email_flow = Flow("emails.priority") | ||
| bulk_email_flow = Flow("emails.bulk") | ||
|
|
||
| default_broker = InMemoryBroker( | ||
| router=router, | ||
| broker_name="default", | ||
| default_flow=default_email_flow, | ||
| await_inplace=True, | ||
| ) | ||
| priority_broker = InMemoryBroker( | ||
| router=router, | ||
| broker_name="priority", | ||
| default_flow=priority_email_flow, | ||
| await_inplace=True, | ||
| ) | ||
|
|
||
|
|
||
| @default_broker.task(task_name="examples.send_email", domain="notifications") | ||
| async def send_email(user_id: int, template: str) -> str: | ||
| """Pretend to render and send an email.""" | ||
| return f"{template} email sent to user {user_id}" | ||
|
|
||
|
|
||
| priority_route = router.route_task( | ||
| send_email, | ||
| broker=priority_broker, | ||
| flow=priority_email_flow, | ||
| ) | ||
|
|
||
|
|
||
| async def _main() -> None: | ||
| await default_broker.startup() | ||
| await priority_broker.startup() | ||
| try: | ||
| direct_result = await send_email(7, "welcome") | ||
|
|
||
| routed_task = await send_email.kiq(7, "welcome") | ||
| routed_result = await routed_task.wait_result(timeout=2) | ||
|
|
||
| bulk_task = ( | ||
| await send_email.kicker() | ||
| .with_route( | ||
| default_broker, | ||
| bulk_email_flow, | ||
| ) | ||
| .kiq(8, "digest") | ||
| ) | ||
| bulk_result = await bulk_task.wait_result(timeout=2) | ||
|
|
||
| print(f"Direct call: {direct_result}") | ||
| print(f"Declared route: {priority_route.broker_name}") | ||
| print(f"Routed call: {routed_result.return_value}") | ||
| print(f"Route override: {bulk_result.return_value}") | ||
| finally: | ||
| await priority_broker.shutdown() | ||
| await default_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,74 @@ | ||
| """Declare shared task definitions and bind them in the final application.""" | ||
|
|
||
| import asyncio | ||
| from collections.abc import Mapping | ||
| from dataclasses import dataclass | ||
|
|
||
| from taskiq import Flow, InMemoryBroker, TaskiqRouter, task_builder | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class BillingQueue: | ||
| """Broker-specific flow that follows the shared flow protocol.""" | ||
|
|
||
| name: str | ||
| priority: int | ||
|
|
||
| def broker_options(self, broker_name: str) -> Mapping[str, object]: | ||
| """Return options that a billing broker adapter can understand.""" | ||
| return { | ||
| "broker": broker_name, | ||
| "priority": self.priority, | ||
| } | ||
|
|
||
|
|
||
| @task_builder("billing.calculate_total", domain="billing") | ||
| async def calculate_total(price: int, quantity: int) -> int: | ||
| """Package-level task definition that is not bound to any broker.""" | ||
| return price * quantity | ||
|
|
||
|
|
||
| router = TaskiqRouter() | ||
| billing_flow = Flow("billing.tasks") | ||
| priority_billing_flow = BillingQueue(name="billing.priority", priority=10) | ||
|
|
||
| billing_broker = InMemoryBroker( | ||
| router=router, | ||
| broker_name="billing", | ||
| default_flow=billing_flow, | ||
| await_inplace=True, | ||
| ) | ||
|
|
||
| registered_calculate_total = router.register_task( | ||
| calculate_total, | ||
| broker=billing_broker, | ||
| flow=billing_flow, | ||
| ) | ||
|
|
||
|
|
||
| async def _main() -> None: | ||
| await billing_broker.startup() | ||
| try: | ||
| direct_result = await calculate_total.call(19, 3) | ||
|
|
||
| prepared_task = ( | ||
| registered_calculate_total.kicker() | ||
| .with_route( | ||
| billing_broker, | ||
| priority_billing_flow, | ||
| ) | ||
| .prepare(19, 3) | ||
| ) | ||
|
|
||
| queued_task = await prepared_task.kiq() | ||
| queued_result = await queued_task.wait_result(timeout=2) | ||
|
|
||
| print(f"Shared task direct call: {direct_result}") | ||
| print(f"Prepared message: {prepared_task.message.task_name}") | ||
| print(f"Registered queued call: {queued_result.return_value}") | ||
| finally: | ||
| await billing_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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It says that the task is routed, but it's very implicit.