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.1.0.tar.gz (4.1 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.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_process_restarter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d97d48b0cd177c1b6c9eb9461ad8dc7afcb5ddcd7f7f6ce01ec32cae49972128
MD5 1eed02b8c0984d1f41653f56b2d8e2d1
BLAKE2b-256 1590193caa0e67f0026ba2f14b629e8f4b03acf71e2d238b5e5f36818a23eca8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_process_restarter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00ad4d9f574afc4c9ed50e2917f59218a75241ebe85459663a6865bd9aa69581
MD5 6001a5e64f1e51848279481f1b8fbfda
BLAKE2b-256 6a9dfa0b08cab8c3b974b6af590a4f875e2b0277fd8ab6791901538ab56266bb

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