https://github.com/alexdemure/gadfastemporal
Example repository demonstrating integration of Temporal workflows with FastAPI
https://github.com/alexdemure/gadfastemporal
fastapi-temporal python-temporal temporal
Last synced: 9 months ago
JSON representation
Example repository demonstrating integration of Temporal workflows with FastAPI
- Host: GitHub
- URL: https://github.com/alexdemure/gadfastemporal
- Owner: AlexDemure
- License: mit
- Created: 2025-04-21T07:07:11.000Z (about 1 year ago)
- Default Branch: production
- Last Pushed: 2025-04-26T08:22:35.000Z (about 1 year ago)
- Last Synced: 2025-08-17T00:24:09.837Z (10 months ago)
- Topics: fastapi-temporal, python-temporal, temporal
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Example repository demonstrating integration of Temporal workflows with FastAPI
---
### Temporal Python
- https://github.com/temporalio/samples-python
- https://github.com/temporalio/sdk-python
### Installation
```
pip install gadfastemporal
```
### Usage
```python
import contextlib
import contextvars
import typing
import uuid
from fastapi import FastAPI
from pydantic import BaseModel
from gadfastemporal import Temporal
from gadfastemporal import Workflow
from gadfastemporal import converters
from gadfastemporal import decorators
from gadfastemporal import interceptors
contextvar = contextvars.ContextVar("contextvar", default=None)
class Settings(BaseModel):
TEMPORAL_HOST: str = "localhost:7233"
TEMPORAL_NAMESPACE: str = "default"
TEMPORAL_QUEUE: str = "my-queue"
settings = Settings()
@decorators.workflow
class SimpleWorkflow(Workflow):
@staticmethod
def id(_: dict) -> str:
return str(uuid.uuid4())
@staticmethod
def activities() -> typing.List[typing.Callable]:
return [SimpleWorkflow.process]
@decorators.run
async def run(self, command: dict) -> str:
return await temporal.activity.sync(self.process, command)
@staticmethod
@decorators.activity
async def process(data: dict) -> str:
return f"Processed: {data}"
temporal = Temporal(
host=settings.TEMPORAL_HOST,
namespace=settings.TEMPORAL_NAMESPACE,
task_queue=settings.TEMPORAL_QUEUE,
workflows=[SimpleWorkflow],
worker_interceptors=[interceptors.SentryInterceptor()],
client_interceptors=[interceptors.ContextPropagationInterceptor(contexts=[contextvar])],
data_converter=converters.pydantic,
search_attributes=[contextvar],
)
@contextlib.asynccontextmanager
async def lifespan(_: FastAPI):
await temporal.start()
yield
await temporal.shutdown()
app = FastAPI(lifespan=lifespan)
@app.post("/run")
async def run():
data = dict(x=1)
contextvar.set({"Traceparent": "12345"})
return await temporal.workflow.sync(SimpleWorkflow, data)
```
Up containers
```
docker compose -f .compose/docker-compose.yml up -d --build
```
Create search-attribute
```
temporal operator search-attribute create --name Traceparent --type Keyword
```
