Skip to main content

FastAPI Process Restarter

一个基于 FastAPI 的通用进程管理和重启工具,支持跨平台(Linux/macOS/Windows)。

功能特性

  • 🚀 一键重启: 通过 HTTP API 重启任何命令/脚本
  • 🔄 自动重启: 进程崩溃后自动恢复(v0.2.0+)
  • 🖥️ 跨平台支持: 原生兼容 Linux、macOS 和 Windows
  • 📊 状态监控: 实时查看进程运行状态和 PID
  • 🧩 易于集成: 可作为子应用挂载到现有 FastAPI 项目
  • 🔧 CLI 工具: 提供命令行工具,开箱即用

安装

pip install fastapi-process-restarter

使用方法

方式一:命令行工具 (CLI)

安装后,直接在终端使用 restarter 命令:

# 基本用法
restarter --cmd "python main.py"

# 指定端口
restarter --cmd "python main.py" --port 9000

# 启用自动重启(进程崩溃后自动恢复)
restarter --cmd "python main.py" -a

# 自定义检查间隔(默认 3 秒)
restarter --cmd "python main.py" -a --check-interval 5.0

# 完整参数示例
restarter -c "node server.js" --host 127.0.0.1 -p 8080 -a --check-interval 2.0

CLI 参数说明:

参数 简写 说明 默认值
--cmd -c 要运行的命令(必需) -
--host - API 监听地址 0.0.0.0
--port -p API 端口 8080
--auto-restart -a 进程崩溃后自动重启 False
--check-interval - 自动重启检查间隔(秒) 3.0

API 端点:

  • POST /restart - 手动重启进程
  • GET /status - 获取进程状态(包含 auto_restart 配置信息)

示例:

# 启动带自动重启的服务
restarter --cmd "python main.py" -a --check-interval 3.0

# 手动重启进程
curl -X POST http://localhost:8080/restart

# 查看状态
curl http://localhost:8080/status
# 输出示例:
# {"running":true,"pid":12345,"auto_restart":true,"check_interval":3.0}

方式二:Python API 集成

在现有 FastAPI 项目中作为子应用挂载:

from fastapi import FastAPI
from process_restarter import create_app

# 你的主应用
main_app = FastAPI()

@main_app.get("/")
def home():
    return {"message": "Hello World"}

# 挂载进程重启器(启用自动重启)
cmd_app = create_app(
    cmd="node server.js",
    auto_restart=True,      # 启用自动重启
    check_interval=5.0      # 每 5 秒检查一次
)
main_app.mount("/subsystem", cmd_app)

# 现在可以通过以下端点操作:
# POST /subsystem/restart  - 手动重启 node server.js
# GET  /subsystem/status   - 查看进程状态

也可以直接使用 ProcessManager:

from process_restarter import ProcessManager

# 创建管理器(启用自动重启)
manager = ProcessManager(
    cmd="python worker.py",
    auto_restart=True,
    check_interval=3.0
)

manager.start()      # 启动进程(同时启动监控线程)
manager.restart()    # 手动重启进程
manager.stop()       # 停止进程和监控线程

技术细节

  • 跨平台进程管理:
    • Linux/macOS: 使用 os.setsid 创建进程组
    • Windows: 使用 CREATE_NEW_PROCESS_GROUP 创建进程组
  • 自动重启机制:
    • 独立守护线程每 N 秒检查进程状态
    • 进程退出时自动重新启动
    • 支持自定义检查间隔
  • 生命周期管理:使用 FastAPI @asynccontextmanager 确保程序退出时子进程被清理
  • 日志桥接:子进程输出实时转发到主进程 stdout

版本历史

v0.2.0

  • 新增自动重启功能 (--auto-restart, -a)
  • 新增检查间隔配置 (--check-interval)
  • /status 接口返回自动重启配置信息

v0.1.0

  • 初始版本
  • 进程启动/停止/重启功能
  • FastAPI 接口支持
  • CLI 命令行工具

许可证

MIT License

Metadata

Release files for fastapi-process-restarter 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-process-restarter 0.2.1
File Size Uploaded Provenance
fastapi_process_restarter-0.2.1.tar.gz 5.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-process-restarter 0.2.1
File Interpreter ABI Platform
fastapi_process_restarter-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / fastapi_process_restarter-0.2.1.tar.gz

Download URL fastapi_process_restarter-0.2.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a15177a6a869f03de79f0429acd04866f8ce2a171e1a2282c3c8714660c11677
BLAKE2b-256 checksum
How to use checksums
36adba4946163ebfbbdee1a377db7c62b36eb46e88530bc3995451f67389a104
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release files / fastapi_process_restarter-0.2.1-py3-none-any.whl

Download URL fastapi_process_restarter-0.2.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87f21f9c1b0584bf1f19d3d238c4cf07b3f4ad560c3852e6ed12e52f0d66cddb
BLAKE2b-256 checksum
How to use checksums
4c66d212d5cbe0d7e4fece91bd94a00bbd733b6b51b55a60ecf433a0932e81ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Security for fastapi-process-restarter 0.2.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page