You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/dynamic-brokers.md
+15-19Lines changed: 15 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,35 +3,31 @@ title: Dynamic Environments
3
3
order: 9
4
4
---
5
5
6
-
This article is for all the people who want to dynamically create brokers, register tasks, and run them inside their code. Or maybe implement more complex logic.
6
+
This article is for people who want to:
7
7
8
-
The Taskiq allows you to create broker instances in all parts of your application. You
9
-
can register tasks dynamically and run them. But when tasks are created dynamically,
10
-
the `taskiq worker` command won't be able to find them.
8
+
* Create brokers dynamically.
9
+
* Register tasks, and run them inside their code.
10
+
* Implement more complex logic.
11
11
12
-
To define tasks and assign them to broker, use `register_task` method.
12
+
Taskiq allows you to set up broker instances throughout your application and register tasks for dynamic execution. However, tasks created this way won't be found by the `taskiq worker` command.
13
+
14
+
To define tasks and assign them to a broker, use `register_task` method.
13
15
14
16
@[code python](../examples/dynamics/broker.py)
15
17
16
-
The problem with this code is that if we run the `taskiq worker` command, it won't be able
17
-
to execute our tasks. Because lambdas are created within the `main` function and they
18
-
are not visible outside of it.
18
+
In this example, the task is defined using a lambda within the `main` function. As the lambda is not visible outside of the `main` function scope, the task is not executable by `taskiq worker` command.
19
+
20
+
To overcome this issue, you can:
19
21
20
-
To surpass this issue, we need to create a dynamic worker task within the current loop.
21
-
Or, we can create a code that can listen to our brokers and have all information about dynamic
22
-
functions.
22
+
* Create a dynamic worker task within the current event loop.
23
+
* Implement your own broker listener with the information about all of your tasks.
23
24
24
-
Here I won't be showing how to create your own CLI command, but I'll show you how to create
25
-
a dynamic worker within the current loop.
25
+
Here's an example of a dynamic worker task creation:
26
26
27
27
@[code python](../examples/dynamics/receiver.py)
28
28
29
-
Here we define a dynamic lambda task with some name, assign it to broker, as we did before.
30
-
The only difference is that we start our receiver coroutine, that will listen to the new
31
-
messages and execute them. Receiver task will be executed in the current loop, and when main function
32
-
exits, the receriver task is canceled. But for illustration purpose, I canceled it manually.
29
+
In this example, a named dynamic lambda task is created and registered in a broker, similar to the previous example. The difference is the creation of a new receiver coroutine for the worker task. It will listen to the new messages and execute them. The worker task will be executed in the current event loop. After exiting the scope, the worker task will get cancelled. For illustration purposes it is cancelled explicitly.
33
30
34
-
Sometimes you need to run not only receiver, but a scheduler as well. You can do it, by using
35
-
another function that also can work within the current loop.
31
+
It's possible to run a scheduler in the current event loop as well:
0 commit comments