Skip to content

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.

The fastest way to get deployment configs is the deploy command. It builds your project and generates platform-specific configuration files.

Terminal window
pywire deploy --platform docker

Docker (default) — generates a Dockerfile:

Terminal window
pywire deploy --platform docker

The generated Dockerfile uses python:3.12-slim, installs dependencies with uv, and runs the app with pywire run.

Render — generates a render.yaml:

Terminal window
pywire deploy --platform render

After 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:

Terminal window
pywire deploy --platform fly

Then deploy with the Fly CLI:

Terminal window
# One-time setup
fly launch --no-deploy # imports fly.toml
# Deploy
fly deploy

To 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:

Terminal window
pywire deploy --platform railway

Railway auto-detects the Dockerfile. Deploy with the Railway CLI:

Terminal window
railway login && railway init
railway up

Cloudflare 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.

Terminal window
pywire deploy --platform cloudflare

The 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:

Terminal window
# Standard PyWire dev server (fast hot-reload, recommended for daily dev)
pywire dev
# Cloudflare Workers local runtime (tests workerd compatibility)
pywire build --platform cloudflare
uv run pywrangler dev

Deploy:

Terminal window
pywire build --platform cloudflare
uv run pywrangler deploy

Architecture notes:

  • Each object runs one thing at a time, like one server process, so it suits the traffic one pywire run --workers 1 process 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.environ before your app is imported, so set PYWIRE_SECRET_KEY and your app’s own settings with wrangler secret put or [vars].
  • --workers and --redis flags 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 generated wrangler.toml sets. Older dates fail when pywrangler installs 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.

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.

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.

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.
  • @submit handlers 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.
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

In non-interactive mode, sessions use the same Redis/memory store as interactive mode. For multi-worker deployments, use Redis:

Terminal window
export REDIS_URL=redis://localhost:6379
app = PyWire(interactive_server_mode=False)
# Redis detected automatically from REDIS_URL

With workers=1 and the default in-memory store, sessions work without Redis — similar to a Django/Rails setup.

  1. Build artifacts: Run pywire build to compile .wire files into optimized Python bytecode.

    Terminal window
    pywire build --optimize
  2. Environment variables: Configure database connection strings, API keys, and other secrets via environment variables or a .env file.

  3. Start the server: Use pywire run for a production-ready Uvicorn-based server.

    Terminal window
    pywire run main:app --host 0.0.0.0 --port 8000 --workers 4
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

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.

Build and run locally:

Terminal window
docker build -t my-pywire-app .
docker run -p 8000:8000 my-pywire-app

Since PyWire is a standard ASGI application, you can use any ASGI server:

Terminal window
# Uvicorn directly
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
# Hypercorn
hypercorn main:app --bind 0.0.0.0:8000 --workers 4

For development with HTTPS, use the SSL flags on pywire dev:

Terminal window
pywire dev --ssl-keyfile key.pem --ssl-certfile cert.pem

In production, terminate SSL at a reverse proxy (Nginx, Caddy, or your cloud provider’s load balancer) rather than at the application level.

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.

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.