Deployment
PyWire applications can be deployed as long-running ASGI services or as stateless FaaS/edge functions. See Edge & serverless deployment to choose a tier and Provider quickstarts for generated platform commands.
pywire deploy
Section titled “pywire deploy”The fastest way to get deployment configs is the deploy command. It builds your project and generates platform-specific configuration files.
pywire deploy --platform dockerPlatforms
Section titled “Platforms”Docker (default) — generates a Dockerfile:
pywire deploy --platform dockerThe generated Dockerfile uses python:3.12-slim, installs dependencies with uv, and runs the app with pywire run.
Render — generates a render.yaml:
pywire deploy --platform renderAfter generating, push to your Git repo and connect it to Render. The render.yaml configures a web service with the correct build and start commands.
Fly.io — generates a fly.toml and a Dockerfile:
pywire deploy --platform flyThen deploy with the Fly CLI:
# One-time setupfly launch --no-deploy # imports fly.toml
# Deployfly deployTo scale to multiple machines (fly scale count N), you need sticky sessions or a shared Redis instance — see Horizontal Scaling.
Railway — generates a railway.json and Dockerfile:
pywire deploy --platform railwayRailway auto-detects the Dockerfile. Deploy with the Railway CLI:
railway login && railway initrailway upCloudflare Workers — generates wrangler.toml, entry.py, and pywire_do.py. For a src.main:app layout, entry.py and pywire_do.py go in src/ (with the build output from pywire build --platform cloudflare), so wrangler uploads only your app, not your tests or virtual environments. After the first run, wrangler.toml is yours: pywire build leaves it alone and warns if it lacks the app’s Durable Object binding.
pywire deploy --platform cloudflareThe app runs in Durable Objects placed near your visitors: one per Cloudflare region (western and eastern North America, South America, western and eastern Europe, Asia-Pacific, Oceania, Africa and the Middle East), created on first use. The Worker sends every page load, WebSocket and long-poll request to the object for the visitor’s region. Each object runs the whole app, the way one pywire run --workers 1 process does: each tab gets its own page, and module-level wires are shared by everyone in that region. Static assets are served from Cloudflare’s edge CDN and never reach an object.
Visitors in different regions don’t share module-level wires, just as two worker processes on a server don’t. To run one object for everyone, so that all visitors share them, set a Worker var in wrangler.toml:
[vars]PYWIRE_PLACEMENT = "global"A single object lives near whoever reached it first, so visitors far from it pay the extra round trip.
This target serves stateful apps, and the build refuses a PyWire(stateless=True) app. Deploy a stateless app to a plain Worker with --platform cloudflare-edge instead (see Provider quickstarts).
Local development:
# Standard PyWire dev server (fast hot-reload, recommended for daily dev)pywire dev
# Cloudflare Workers local runtime (tests workerd compatibility)pywire build --platform cloudflareuv run pywrangler devDeploy:
pywire build --platform cloudflareuv run pywrangler deployArchitecture notes:
- Each object runs one thing at a time, like one server process, so it suits the traffic one
pywire run --workers 1process can take. - An object stays in memory while anyone is connected to it. When everyone has left, or after a deploy, Cloudflare may evict it, and its module-level state and open pages start over, as after a server restart.
- Worker variables and secrets reach
os.environbefore your app is imported, so setPYWIRE_SECRET_KEYand your app’s own settings withwrangler secret putor[vars]. --workersand--redisflags are not applicable.
Current limitations:
- Cold starts (2-4 seconds): Cloudflare Python Workers have cold start latency from restoring the Pyodide (WebAssembly Python) snapshot. Only the first visitor in a region after its object starts waits for it; later page loads and sockets reuse the running object.
- Pydantic needs a recent compatibility date:
pywire[forms](pydantic) runs on Python Workers from compatibility date 2026-09-01 (Pyodide 0.28), which the generatedwrangler.tomlsets. Older dates fail whenpywranglerinstalls the dependencies. - Platform maturity: Cloudflare Python Workers launched in late 2024 and is actively being improved. Cold start performance is expected to improve as Cloudflare optimizes Pyodide snapshot restoration and Durable Object initialization. Follow Cloudflare’s Python Workers changelog for updates.
For latency-sensitive applications, container-based deployments (Docker, Railway, Render, Fly.io) provide sub-100ms WebSocket connections with no cold start penalty.
Options
Section titled “Options”| Flag | Description |
|---|---|
--platform |
Target platform: docker, render, fly, railway, or cloudflare (default: docker) |
--workers |
Number of worker processes — not applicable to cloudflare (default: 1) |
--redis |
Include Redis/Valkey KV store in deployment config — not applicable to cloudflare |
--out-dir |
Output directory for generated files (default: .) |
Use --redis --workers 4 to generate configs pre-configured for multi-worker scaling. See Horizontal Scaling for details.
The command validates your project before generating configs. If pyproject.toml or uv.lock is missing, you’ll see a warning.
HTTP-only session mode
Section titled “HTTP-only session mode”For a long-running host that cannot maintain persistent WebSocket connections, PyWire can use HTTP-session transport. This is different from FaaS stateless mode: HTTP-session still stores page state in a session cookie plus the configured session store.
from pywire import PyWire
app = PyWire(interactive_server_mode=False)Use this mode for a container/server without WebSocket support. For Lambda, Azure Functions, GCP Cloud Functions, or a plain Cloudflare Worker, use PyWire(stateless=True) instead; those targets are request-scoped and do not keep session state between invocations.
How It Works
Section titled “How It Works”When interactive_server_mode=False:
- No WebSocket endpoint is registered. No persistent connections.
- SPA navigation uses
fetch()— the client fetches new page HTML via HTTP and applies it with morphdom (same smooth transitions). - Session state is persisted via a signed httponly cookie and the session store (in-memory or Redis). State survives across requests.
@submithandlers work via standard form POST. The server calls the handler, re-renders the page, and returns HTML.@click,@input, etc. are silently inactive — a dev-mode console warning lists which handlers won’t work.
When to Use It
Section titled “When to Use It”| Scenario | Mode |
|---|---|
| Traditional server deployment (VPS, containers) | interactive_server_mode=True (default) |
| Serverless functions (AWS Lambda, Azure, GCP) | stateless=True (FaaS is request-scoped) |
| Plain Cloudflare Worker | stateless=True |
| Cloudflare Durable Objects | Default stateful WebSocket mode |
| Horizontal scaling without Redis | interactive_server_mode=False |
| Static/content sites with forms | interactive_server_mode=False |
Session Configuration
Section titled “Session Configuration”In non-interactive mode, sessions use the same Redis/memory store as interactive mode. For multi-worker deployments, use Redis:
export REDIS_URL=redis://localhost:6379app = PyWire(interactive_server_mode=False)# Redis detected automatically from REDIS_URLWith workers=1 and the default in-memory store, sessions work without Redis — similar to a Django/Rails setup.
Preparing for Production
Section titled “Preparing for Production”-
Build artifacts: Run
pywire buildto compile.wirefiles into optimized Python bytecode.Terminal window pywire build --optimize -
Environment variables: Configure database connection strings, API keys, and other secrets via environment variables or a
.envfile. -
Start the server: Use
pywire runfor a production-ready Uvicorn-based server.Terminal window pywire run main:app --host 0.0.0.0 --port 8000 --workers 4
Production Flags
Section titled “Production Flags”| Flag | Description |
|---|---|
--host 0.0.0.0 |
Bind to all interfaces (required for containers) |
--port 8000 |
Set the port (default: 8000) |
--workers N |
Number of worker processes (default: auto based on CPU cores) |
--no-access-log |
Disable access logging for better performance |
Manual Deployment
Section titled “Manual Deployment”If you prefer to write deployment configs by hand or use a platform not supported by pywire deploy, PyWire works with any ASGI-compatible hosting.
Docker
Section titled “Docker”Build and run locally:
docker build -t my-pywire-app .docker run -p 8000:8000 my-pywire-appGeneric ASGI Deployment
Section titled “Generic ASGI Deployment”Since PyWire is a standard ASGI application, you can use any ASGI server:
# Uvicorn directlyuvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
# Hypercornhypercorn main:app --bind 0.0.0.0:8000 --workers 4SSL / HTTPS
Section titled “SSL / HTTPS”For development with HTTPS, use the SSL flags on pywire dev:
pywire dev --ssl-keyfile key.pem --ssl-certfile cert.pemIn production, terminate SSL at a reverse proxy (Nginx, Caddy, or your cloud provider’s load balancer) rather than at the application level.
Compression
Section titled “Compression”PyWire gzips text responses itself: pages, the client runtime, CSS, JSON and HTTP-transport updates. The client runtime is compressed once and cached, so a cold load of a small page is about 25 KB instead of 84 KB. Images, fonts and other already-compressed files pass through untouched.
If a CDN or reverse proxy in front of the app already compresses, turn it off to save the CPU:
app = PyWire(compress=False)WebSocket messages use the browser’s permessage-deflate, negotiated by Uvicorn. See pywire run --no-ws-deflate in the CLI guide for the memory trade-off.
Static Files
Section titled “Static Files”PyWire automatically serves files from the static/ directory at the /static URL prefix. In production, consider serving static files from a CDN or reverse proxy for better performance.