Last active
August 16, 2021 21:36
-
-
Save daviskirk/e2f885288f3108aa4b17cee41bd8c883 to your computer and use it in GitHub Desktop.
FastAPI + Celery Development Server
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
| pip install fastapi celery[redis] uvicorn |
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
| import celery | |
| from fastapi import FastAPI | |
| import tasks | |
| app = FastAPI() | |
| @app.get("/") | |
| def read_root(): | |
| result = tasks.add.delay(2, 2) | |
| return {"2+2": result.get()} |
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
| import uvicorn | |
| original_callback = uvicorn.main.callback | |
| def callback(**kwargs): | |
| from celery.contrib.testing.worker import start_worker | |
| import tasks | |
| with start_worker(tasks.app, perform_ping_check=False, loglevel="info"): | |
| original_callback(**kwargs) | |
| uvicorn.main.callback = callback | |
| if __name__ == "__main__": | |
| uvicorn.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
| python run.py main:app --reload |
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
| from celery import Celery | |
| app = Celery("tasks", broker='redis://localhost:6379/0', backend='redis://localhost') | |
| @app.task | |
| def add(x, y): | |
| return x + y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment