This project bind the config and transaction from pyramid to temporal
Project description
pyramid-temporal
pyramid-temporal provides automatic transaction management for Temporal activities using pyramid_tm, exactly how it works for web requests.
This library gives Temporal activities real Pyramid requests (via pyramid.scripting.prepare), so all your existing request methods work automatically - request.dbsession, request.tm, and any other methods configured via add_request_method.
- Github repository: https://github.com/cartaorobbin/pyramid-temporal/
- Documentation https://cartaorobbin.github.io/pyramid-temporal/
Features
- Real Pyramid Requests: Activities get actual
pyramid.request.Requestobjects, not mocks - Automatic Transaction Management: Uses
pyramid_tm- same as web requests - Full Pyramid Integration: All
add_request_methodconfigurations work automatically - PyramidEnvironment: Clean wrapper for bootstrap environment with access to app, registry, root
- Unit of Work Pattern: Each activity runs in its own transactional scope with fresh request
- Clean Activity Code: No manual transaction handling or context setup needed
- Custom Worker:
pyramid_temporal.Workerhandles context binding automatically
Quick Start
Installation
pip install pyramid-temporal
Basic Usage
from temporalio import workflow
from temporalio.client import Client
from pyramid_temporal import Worker, activity, ActivityContext, PyramidEnvironment
# Define activities with automatic context injection
@activity.defn
async def enrich_user(context: ActivityContext, user_id: int) -> bool:
"""Activity with full Pyramid integration.
context.request is a REAL Pyramid Request object with all
configured request methods (dbsession, tm, etc.) available.
"""
# Access database session - transactions are automatic!
session = context.request.dbsession
user = session.query(User).get(user_id)
if user:
user.enriched = True
# No need to commit - pyramid_tm handles it on success
return True
return False
@activity.defn
async def send_notification(context: ActivityContext, user_id: int, message: str) -> bool:
"""Another activity using context."""
# Access settings from the real request
api_key = context.request.registry.settings.get('notification.api_key')
# ... send notification
return True
@workflow.defn(sandboxed=False)
class UserOnboardingWorkflow:
@workflow.run
async def run(self, user_id: int) -> bool:
# Enrich user data
await workflow.execute_activity(
enrich_user, user_id,
schedule_to_close_timeout=timedelta(seconds=60)
)
# Send welcome notification
await workflow.execute_activity(
send_notification, user_id, "Welcome!",
schedule_to_close_timeout=timedelta(seconds=60)
)
return True
Worker Setup
from pyramid_temporal import Worker, PyramidEnvironment
def create_worker(env: PyramidEnvironment):
"""Create worker with Pyramid integration.
Args:
env: PyramidEnvironment from bootstrap (provided by CLI)
"""
client = env.registry.get('temporal_client')
# Worker automatically binds activities to context
worker = Worker(
client,
env, # Full Pyramid environment
task_queue="my-queue",
activities=[enrich_user, send_notification],
workflows=[UserOnboardingWorkflow],
)
return worker
Pyramid Configuration
In your Pyramid application, configure as you normally would for web requests:
from pyramid.config import Configurator
def main(global_config, **settings):
config = Configurator(settings=settings)
# Standard Pyramid/pyramid_tm setup
config.include('pyramid_tm')
config.include('pyramid_temporal')
# Configure request.dbsession as you normally would
config.add_request_method(
lambda r: get_tm_session(session_factory, r.tm),
'dbsession',
reify=True
)
return config.make_wsgi_app()
The same configuration works for both web requests and Temporal activities!
CLI Usage
Start workers using the CLI command:
ptemporal-worker development.ini myapp.workers.create_worker
How It Works
pyramid-temporal uses pyramid.scripting.prepare to give activities real Pyramid requests:
- Bootstrap →
PyramidEnvironmentwraps the full Pyramid bootstrap (app, registry, root) - Activity Starts → Real Pyramid Request created via
pyramid.scripting.prepare - Context Injected →
ActivityContextprovides access to real request with all methods - Activity Succeeds → Transaction commits automatically (via
pyramid_tm) - Activity Fails → Transaction aborts automatically
- Cleanup → Request context closed via
prepare()'s closer
This is exactly how pyramid_tm works for web requests - your activities use the same patterns.
API Reference
@activity.defn
Decorator to define a pyramid-temporal activity with context injection:
@activity.defn
async def my_activity(context: ActivityContext, arg1: str, arg2: int) -> bool:
session = context.request.dbsession
# ...
PyramidEnvironment
Wrapper for the Pyramid bootstrap environment:
from pyramid.paster import bootstrap
from pyramid_temporal import PyramidEnvironment
# Create from bootstrap output
env = PyramidEnvironment.from_bootstrap(bootstrap('development.ini'))
# Access components
env.registry # Pyramid registry
env.app # WSGI application
env.request # Base request object
env.root # Root object (for traversal)
env.settings # Shortcut to registry.settings
# Clean up when done
env.close()
ActivityContext
Context object passed to activities:
context.env- FullPyramidEnvironmentcontext.registry- Pyramid registry (shortcut toenv.registry)context.settings- Application settings (shortcut toenv.settings)context.request- Real Pyramid Request with all configured methods:request.dbsession- if configured viaadd_request_methodrequest.tm- ifpyramid_tmis included- Any other methods you've configured
Worker
Pyramid-aware Temporal worker:
worker = Worker(
client, # Temporal client
env, # PyramidEnvironment (required)
task_queue="...", # Task queue name
activities=[...], # List of activities
workflows=[...], # List of workflows
)
Development
See .dev-local/README.md for development setup instructions.
Inspiration
This library is inspired by pyramid_tm, which provides excellent transaction management for Pyramid web applications. We apply the same pattern to Temporal activities.
Repository initiated with fpgmaas/cookiecutter-poetry.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyramid_temporal-0.0.4.tar.gz.
File metadata
- Download URL: pyramid_temporal-0.0.4.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d8a0b33f323393fcee9e7a925d55d693fae5d43be9ac26ea6675e9daa665ac1
|
|
| MD5 |
15d853b21136e8c7a3df432be4d4f493
|
|
| BLAKE2b-256 |
534804923544d52f153ccf8cf77c5effa10adda6c2174b0d3a561354f1256325
|
File details
Details for the file pyramid_temporal-0.0.4-py3-none-any.whl.
File metadata
- Download URL: pyramid_temporal-0.0.4-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20a331954a681089f6e24217c49c4d57ff10cea1b1b82d4b707693fb14098e02
|
|
| MD5 |
0d5bfa126fc8fad84d6f56ef6de9ad62
|
|
| BLAKE2b-256 |
2efde5a0dc935023351fc55b6c675a3698eab436e9a71444fdbe7e52a99487d4
|