Runlane
Recipes

Run Python

Analyze data with Python and return structured results.

Runlane executes a TypeScript task while a worker you control starts Python. Your deployment supplies Python, packages, scripts, input files, and resource limits.

Run a bounded analysis

This task starts a Python analysis script with an explicit argument array, a five-minute timeout, cancellation, and a bounded output buffer:

src/tasks/analyze-data.ts
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'

import { task } from '@runlane/core'
import * as z from 'zod'

const execFileAsync = promisify(execFile)
const analysisSchema = z.object({
  chartData: z.array(z.object({ label: z.string(), value: z.number() })),
  insights: z.array(z.string()),
  summary: z.string(),
})

export const analyzeData = task({
  id: 'data.analyze-with-python',
  schema: z.object({ dataUrl: z.url() }),
  output: analysisSchema,
  async run({ dataUrl }, context) {
    const { stdout } = await execFileAsync('python3', ['scripts/analyze.py', dataUrl], {
      encoding: 'utf8',
      maxBuffer: 1024 * 1024,
      signal: context.signal,
      timeout: 5 * 60_000,
    })

    return analysisSchema.parse(JSON.parse(stdout))
  },
})

The Python script prints one JSON object containing a summary, insights, and chart data. The task parses and validates that object before storing it as the run output.

Use execFile rather than building a shell command from task input. Validate every URL, path, flag, and output before it crosses the process boundary.

Package the worker

Your deployment must include:

  • the expected Python version;
  • a locked dependency set;
  • the referenced script or module;
  • enough CPU, memory, and temporary disk;
  • a non-root runtime user where practical.

For large input and output, use object storage instead of the run record or unbounded stdout.

Before production

  • Test missing executables, non-zero exits, invalid JSON, timeouts, and cancellation.
  • Bound stdout and stderr or redirect them to files.
  • Do not pass secrets on a command line visible to other processes.
  • Clean temporary files in finally.
  • Decide which failures are safe to retry.

Runlane does not install Python or build the worker image. See run workers.

On this page