Example repository demonstrating integration of Temporal workflows with FastAPI
Temporal Python
Installation
pip install gadfastemporal
Usage
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
Metadata
Release files for gadfastemporal 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gadfastemporal-0.0.1.tar.gz | 10.0 kB | Without provenance | Details for gadfastemporal-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gadfastemporal-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for gadfastemporal-0.0.1-py3-none-any.whl |
Total release size: 24.2 kB
Release files / gadfastemporal-0.0.1.tar.gz
| Download URL | gadfastemporal-0.0.1.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6cf463554b8657670ac17c1c88056c7d06ff3bcf7f94bc89721871ac2796db14
|
|
BLAKE2b-256 checksum How to use checksums |
900f3c46be56069e2987d26d91d6a6dbe37962ce5c764bf5e652fb955d74bd3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / gadfastemporal-0.0.1-py3-none-any.whl
| Download URL | gadfastemporal-0.0.1-py3-none-any.whl |
|---|---|
| Size | 14.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f5fc54436cc436f4c8a05e086f46e0a14832db81db52452126923e2ff6e25a57
|
|
BLAKE2b-256 checksum How to use checksums |
3f03106d19095bdffc60c8f4053c221ef5885cde9bf1e034edabe9203fdae99a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Security for gadfastemporal 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.