Skip to main content

A FastAPI based wrapper to auto-run and API-restart any terminal command.

Project description

FastAPI Process Restarter

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

功能特性

  • 🚀 一键重启: 通过 HTTP API 重启任何命令/脚本
  • 🖥️ 跨平台支持: 原生兼容 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 "node server.js" --host 127.0.0.1 --port 8080

API 端点:

  • POST /restart - 重启进程
  • GET /status - 获取进程状态

示例:

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

# 查看状态
curl http://localhost:8080/status

方式二: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")
main_app.mount("/subsystem", cmd_app)

# 现在可以通过 POST /subsystem/restart 重启 node server.js

也可以直接使用 ProcessManager

from process_restarter import ProcessManager

manager = ProcessManager(cmd="python worker.py")
manager.start()      # 启动进程
manager.restart()    # 重启进程
manager.stop()       # 停止进程

技术细节

  • 跨平台进程管理:
    • Linux/macOS: 使用 os.setsid 创建进程组
    • Windows: 使用 CREATE_NEW_PROCESS_GROUP 创建进程组
  • 生命周期管理: 使用 FastAPI @asynccontextmanager 确保程序退出时子进程被清理
  • 日志桥接: 子进程输出实时转发到主进程 stdout

许可证

MIT License

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

fastapi_process_restarter-0.2.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_process_restarter-0.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_process_restarter-0.2.0.tar.gz.

File metadata

File hashes

Hashes for fastapi_process_restarter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 639c99c57d84f877e6af11b8bc65054ecc05ab3c56d9607f79fc45e1d1e80cac
MD5 58c5402453e24c72fb3b8ed8fa98398f
BLAKE2b-256 f6a0e40d5254a71ea1bb88923abb2777cae7420829b4f794ec5676c2397e9066

See more details on using hashes here.

File details

Details for the file fastapi_process_restarter-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_process_restarter-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e8635c0b5843a0faeedb6228b888c6dd4be013bec098e574d3870c85471181f
MD5 a6c034efbffbcd63f960e900d5d408f4
BLAKE2b-256 878262bf545ccecd23745fbe0ddabb13f9183b50235de7b426299ef9cf78edc4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page