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
@@ -1399,7 +1384,7 @@ For `client-side caching`, all you have to do is let the `Settings` class define
1399
1384
1400
1385
Depending on the problem your API is solving, you might want to implement a job queue. A job queue allows you to run tasks in the background, and is usually aimed at functions that require longer run times and don't directly impact user response in your frontend. As a rule of thumb, if a task takes more than 2 seconds to run, can be executed asynchronously, and its result is not needed for the next step of the user's interaction, then it is a good candidate for the job queue.
1401
1386
1402
-
> [!TIP]
1387
+
> \[!TIP\]
1403
1388
> Very common candidates for background functions are calls to and from LLM endpoints (e.g. OpenAI or Openrouter). This is because they span tens of seconds and often need to be further parsed and saved.
1404
1389
1405
1390
#### Background task creation
@@ -1418,6 +1403,7 @@ Then add the function to the `WorkerSettings` class `functions` variable in `app
1418
1403
from .functions import sample_background_task
1419
1404
from .your_module import sample_complex_background_task
And finally run the worker in parallel to your fastapi application.
1444
1430
1445
-
> [!IMPORTANT]
1431
+
> \[!IMPORTANT\]
1446
1432
> For any change to the `sample_background_task` to be reflected in the worker, you need to restart the worker (e.g. the docker container).
1447
1433
1448
1434
If you are using `docker compose`, the worker is already running.
@@ -1462,6 +1448,7 @@ To do this, you can add the database session to the `ctx` object in the `startup
1462
1448
from arq.worker import Worker
1463
1449
from ...core.db.database import async_get_db
1464
1450
1451
+
1465
1452
asyncdefstartup(ctx: Worker) -> None:
1466
1453
ctx["db"] =awaitanext(async_get_db())
1467
1454
logging.info("Worker Started")
@@ -1477,17 +1464,16 @@ This will allow you to have the async database session always available in any b
1477
1464
```python
1478
1465
from arq.worker import Worker
1479
1466
1467
+
1480
1468
asyncdefyour_background_function(
1481
1469
ctx: Worker,
1482
1470
post_id: int,
1483
-
...
1484
1471
) -> Any:
1485
1472
db = ctx["db"]
1486
1473
post = crud_posts.get(db=db, schema_to_select=PostRead, id=post_id)
1487
-
...
1488
1474
```
1489
1475
1490
-
> [!WARNING]
1476
+
> \[!WARNING\]
1491
1477
> When using database sessions, you will want to use Pydantic objects. However, these objects don't mingle well with the seralization required by ARQ tasks and will be retrieved as a dictionary.
1492
1478
1493
1479
### 5.11 Rate Limiting
@@ -1661,6 +1647,7 @@ This authentication setup in the provides a robust, secure, and user-friendly wa
1661
1647
The boilerplate includes a powerful web-based admin interface built with [CRUDAdmin](https://github.com/benavlabs/crudadmin) that provides a comprehensive database management system.
1662
1648
1663
1649
> **About CRUDAdmin**: CRUDAdmin is a modern admin interface generator for FastAPI applications. Learn more at:
0 commit comments