How to parallel run multiple background task? #6671
-
Code snippet:from fastapi import APIRouter, Depends, BackgroundTasks @logger.catch(level='ERROR') some base code. """ DescriptionI want to run ctask function and vuln_verifier function background task at the same time, but it run one by one. Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
Hi, I didn't find easy solution, but based on documentation you can use Celery and message broker. |
Beta Was this translation helpful? Give feedback.
-
|
@gyKa Thank you for your answer. I have two methods now. |
Beta Was this translation helpful? Give feedback.
-
|
https://apscheduler.readthedocs.io/en/stable/index.html |
Beta Was this translation helpful? Give feedback.
-
But it seems that just can scheduler tasks periodically, can not parallel excute different task in one time. |
Beta Was this translation helpful? Give feedback.
-
|
If the functions you want to run are For |
Beta Was this translation helpful? Give feedback.
If the functions you want to run are
async, you can use the approach described in this comment: #10682 (comment)For
syncfunctions you can modify that code to run functions in thread pool and gather and await results