-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update docs and simplified external trigger API
- Loading branch information
1 parent
4d2df16
commit 069b740
Showing
6 changed files
with
126 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,36 @@ | ||
You can basically run any tasks on aioclock, it could be your redis broker or other kind of brokers listening to a queue. The benefit of doing so, is that you don't need to worry about dependency injection, shutdown or startup event. | ||
|
||
AioClock offer you a unique easy way to spin up new services, without any overhead or perfomance issue! | ||
|
||
```python | ||
from aioclock import AioClock, Forever, OnShutDown | ||
from functools import lru_cache | ||
from your_module import BrokerType | ||
|
||
app = AioClock() | ||
|
||
# your singleton redis instance | ||
@lru_cache | ||
def get_redis() -> BrokerType: | ||
... | ||
|
||
|
||
@app.task(trigger=Forever()) | ||
async def read_message_queue(redis: BrokerType = Depends(get_redis)): | ||
async for message in redis.listen("..."): | ||
... | ||
|
||
|
||
@app.task(trigger=OnShutDown()) | ||
async def shutdown_event(redis: BrokerType = Depends(get_redis)): | ||
await redis.disconnect() | ||
``` | ||
|
||
One other way to do this, is to implement a trigger that automatically execute the function. | ||
But to do so, I basically need to wrap redis in my own library, and that's not good for some reasons: | ||
|
||
1. Complexity of framework increases. | ||
2. Is not realy flexible, because native library and client are always way more flexible. I end up writing something like `Celery`. | ||
3. The architecture I choose to handle interactions with broker may not satisfy your requirement. | ||
|
||
[This repository is an example how you can write a message queue in aioclock.](https://github.com/ManiMozaffar/typed-redis) |
This file contains 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,33 @@ | ||
To run AioClock with FastAPI, you can run it on background with FastAPI lifespan, next to your asgi. | ||
|
||
```python | ||
from aioclock import AioClock | ||
from fastapi import FastAPI | ||
import asyncio | ||
|
||
clock_app = AioClock() | ||
|
||
async def lifespan(app: FastAPI): | ||
task = asyncio.create_task(clock_app.serve()) | ||
yield | ||
|
||
try: | ||
task.cancel() | ||
await task | ||
except asyncio.CancelledError: | ||
... | ||
|
||
app = FastAPI(lifespan=lifespan) | ||
``` | ||
|
||
!!! danger "This setup is not recommended at all" | ||
|
||
Running AioClock with FastAPI is not a good practice in General, because: | ||
FastAPI is a framework to write stateless API, but aioclock is still stateful component in your architecture. | ||
In simlper term, it means if you have 5 instances of aioclock running, they produce 5x tasks than you intended. | ||
So you cannot easily scale up horizontally by adding more aioclock power! | ||
|
||
Even in this case, if you serve FastAPI with multiple process, you end up having one aioclock per each process! | ||
|
||
What I suggest to do is spin one new service, that is responsible to process the periodic taks. | ||
Try to avoid periodic task in general, but sometimes it's not easy to do so. |
This file contains 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