Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions docs/examples/django.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Django Integration Example

This example shows how to integrate FluxQueue with a Django project to offload background work from your views.

## Prerequisites

- A Django project (created with `django-admin startproject`)
- A running **Redis** instance
- FluxQueue installed with the worker

## Project Structure

For a minimal Django example, you can use:

```bash
myproject/
manage.py
myproject/
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
app/
__init__.py
views.py
tasks/
__init__.py
tasks.py
```

## Define Tasks

Create `app/tasks/tasks.py` and define your FluxQueue instance and tasks:

```py
from fluxqueue import FluxQueue

fluxqueue = FluxQueue()


@fluxqueue.task()
async def send_welcome_email(email: str):
# Replace this with your real email sending logic
print(f"Sending welcome email to {email}")
```

## Expose the Task

In order to expose tasks we modify `myproject/app/tasks/__init__.py` like this:

```py
__all__ = ["send_welcome_email"]

from .tasks import send_welcome_email
```

## Use Tasks in Django Views

In `app/views.py`, enqueue tasks from your views instead of doing work inline:

```python
from django.http import JsonResponse

from .tasks import send_welcome_email


async def signup(request):
email = request.POST["email"]

await send_welcome_email(email=email)

return JsonResponse({"status": "ok"})
```

Make sure your `signup` view is added in `urls.py` as usual.

## Run the Worker

Start the FluxQueue worker in a separate process so it can execute tasks:

```bash
fluxqueue start --tasks-module-path app.tasks --queue default
```

## Run the Django Development Server

Run Django with Uvicorn:

```bash
uvicorn myproject.asgi:application
```

Now when you submit the signup form that posts to `signup`, the `send_welcome_email` task will be enqueued and processed by the worker.
85 changes: 85 additions & 0 deletions docs/examples/fastapi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# FastAPI Integration Example

This example shows how to integrate FluxQueue with a FastAPI application to enqueue background work from your HTTP handlers.

## Prerequisites

- **FastAPI** installed
- A running **Redis** instance
- FluxQueue installed with the worker

## Project Structure

For a minimal FastAPI example, you can use:

```bash
my_app/
__init__.py
main.py
tasks/
__init__.py
tasks.py
```

## Define Tasks

Create `tasks/tasks.py` and define your FluxQueue instance and tasks:

```py
from fluxqueue import FluxQueue

fluxqueue = FluxQueue()


@fluxqueue.task()
async def send_welcome_email(email: str):
# Replace this with your real email sending logic
print(f"Sending welcome email to {email}")
```

## Expose the Task

In order to expose the task we modify `my_app/tasks/__init__.py` like this:

```py
__all__ = ["send_welcome_email"]

from .tasks import send_welcome_email
```

## Create the FastAPI App

In `main.py`, wire FluxQueue into your FastAPI routes:

```py
from fastapi import FastAPI

from .tasks import fluxqueue, send_welcome_email

app = FastAPI()


@app.post("/signup")
async def signup(email: str):
await send_welcome_email(email=email)

return {"status": "ok"}
```

## Run the Worker

Start the FluxQueue worker in a separate process so it can execute tasks:

```bash
fluxqueue start --tasks-module-path my_app.tasks --queue default
```

## Run the FastAPI App

Run the FastAPI server with Uvicorn:

```bash
uvicorn my_app.main:app --reload
```

Now when you `POST` to `/signup`, the `send_welcome_email` task will be enqueued and processed by the worker.
87 changes: 87 additions & 0 deletions docs/examples/flask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Flask Integration Example

This example shows how to integrate FluxQueue with a Flask application to offload background work from your HTTP handlers.

## Prerequisites

- **Flask** installed
- A running **Redis** instance
- FluxQueue installed with the worker

## Project Structure

For a minimal Flask example, you can use:

```bash
my_app/
__init__.py
app.py
tasks/
__init__.py
tasks.py
```

## Define Tasks

Create `tasks/tasks.py` and define your FluxQueue instance and tasks:

```py
from fluxqueue import FluxQueue

fluxqueue = FluxQueue()


@fluxqueue.task()
def send_welcome_email(email: str) -> None:
# Replace this with your real email sending logic
print(f"Sending welcome email to {email}")
```

## Expose the Task

In order to expose the task we modify `my_app/tasks/__init__.py` like this:

```py
__all__ = ["send_welcome_email"]

from .tasks import send_welcome_email
```

## Create the Flask App

In `app.py`, wire FluxQueue into your Flask routes:

```py
from flask import Flask, request, jsonify

from .tasks import send_welcome_email

app = Flask(__name__)


@app.post("/signup")
def signup():
email = request.form["email"]

send_welcome_email(email=email)

return jsonify({"status": "ok"})
```

## Run the Worker

Start the FluxQueue worker in a separate process so it can execute tasks:

```bash
fluxqueue start --tasks-module-path my_app.tasks --queue default
```

## Run the Flask App

Run the Flask development server:

```bash
flask --app my_app.app run --debug
```

Now when you `POST` to `/signup`, the `send_welcome_email` task will be enqueued and processed by the worker.
9 changes: 9 additions & 0 deletions docs/examples/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Examples

This section contains practical examples that demonstrate how to use FluxQueue in real projects.

## Sections

- **[Flask Integration](flask.md)** - Using FluxQueue with a Flask application
- **[Django Integration](django.md)** - Using FluxQueue with a Django application
- **[FastAPI Integration](fastapi.md)** - Using FluxQueue with a FastAPI application
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Welcome to FluxQueue documentation. FluxQueue is a lightweight, resource-efficie
- [Defining and Exposing Tasks](tutorial/defininig_and_exposing_tasks.md) - Organize and expose tasks for the worker
- [Worker Setup](tutorial/worker.md) - Deploy and run workers
- [How it Works](how-it-works/index.md) - Learn more about how FluxQueue actually works
- [Examples](examples/index.md) - FluxQueue examples with **Flask**, **Django**, **FastAPI**
- [API Reference](api/index.md) - Complete API documentation

## What is FluxQueue?
Expand Down
13 changes: 0 additions & 13 deletions examples/fastapi/README.md

This file was deleted.

Empty file removed examples/fastapi/src/__init__.py
Empty file.
Empty file.
17 changes: 0 additions & 17 deletions examples/fastapi/src/email/core.py

This file was deleted.

12 changes: 0 additions & 12 deletions examples/fastapi/src/main.py

This file was deleted.

3 changes: 0 additions & 3 deletions examples/fastapi/src/tasks/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions examples/fastapi/src/tasks/core.py

This file was deleted.

16 changes: 0 additions & 16 deletions examples/fastapi/src/tasks/email.py

This file was deleted.

5 changes: 5 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ nav:
- how-it-works/index.md
- how-it-works/client.md
- how-it-works/worker.md
- Examples:
- examples/index.md
- examples/flask.md
- examples/django.md
- examples/fastapi.md
- API Reference:
- api/index.md
- api/fluxqueue.md
Expand Down
Loading
Loading