当前位置: 首页 > news >正文

Flask使用Celery与多进程管理:优雅处理长时间任务与子进程终止技巧(multiprocessing)(subprocess)

在许多任务处理系统中,我们需要使用异步任务队列来处理繁重的计算或长时间运行的任务,如模型训练。Celery是一个广泛使用的分布式任务队列,而在某些任务中,尤其是涉及到调用独立脚本的场景中,我们需要混合使用multiprocessingsubprocess模块来启动和管理这些任务进程。然而,这种组合有时会带来一些挑战,如进程冲突和子进程无法正确终止的问题。

本文将讨论如何使用Celery、Multiprocessing和Subprocess来处理这些问题,并在需要时正确关闭子进程,实现完美的进程管理与切换。

问题描述

当通过celery.control.revoke来终止Celery任务时,如果任务启动了多个子进程,例如使用multiprocessingsubprocess模块,这些子进程不会被立即终止。在某些情况下,子进程会继续运行,导致任务无法彻底停止,并可能造成系统资源浪费。

解决方案

我们可以通过组合使用psutil库来实现对子进程的监控和终止,从而确保所有相关的进程都能正确关闭。以下是具体实现步骤。

安装必要的库

确保你已经安装psutil库:

pip install psutil

修改代码实现

1. Celery任务与Multiprocessing结合Subprocess

首先,我们创建一个Celery任务。当任务启动时,它会使用multiprocessing模块启动一个新的进程,该进程将执行独立的Python脚本。代码如下:

import json
import os
import psutil
import multiprocessing
import subprocess
from celery_app import celeryimport torch.multiprocessing as mpmp.set_start_method('spawn', True)def run_script(json_test_path, uid):command = f"python training.py {json_test_path}"process = subprocess.Popen(command, shell=True)print("===========================================PID:", process.pid)print("===========================================uid:", uid)process.wait()return process.pid@celery.task(bind=True)
def lora_train_task(self, json_test_demo):# 将json_test保存到临时文件中json_test_path = f"training_config_{json_test_demo['uid']}.json"json_test_path = os.path.abspath(json_test_path)with open(json_test_path, 'w') as f:json.dump(json_test_demo, f)# 使用多进程调用独立脚本p = multiprocessing.Process(target=run_script, args=(json_test_path, json_test_demo['uid']))p.start()p.join()return 0
2. 使用psutil关闭子进程

我们通过调用psutil库来监控并关闭所有相关的子进程。以下是实现终止任务和子进程的代码示例:

import psutildef terminate_process_tree(pid):try:parent = psutil.Process(pid)for child in parent.children(recursive=True):  # This will recursively find all child processeschild.terminate()parent.terminate()except psutil.NoSuchProcess:pass# 示例:终止任务时调用终止子进程函数
celery.control.revoke(args['task_id'], terminate=True, signal='SIGKILL')
if args['pid_id']:terminate_process_tree(int(args['pid_id']))

完整示例

将上述代码组合起来,我们得到完成的实现。如下所示:

import json
import os
import psutil
import multiprocessing
import subprocess
from celery_app import celery
import torch.multiprocessing as mpmp.set_start_method('spawn', True)def run_script(json_test_path, uid):command = f"python training.py {json_test_path}"process = subprocess.Popen(command, shell=True)print("===========================================PID:", process.pid)print("===========================================uid:", uid)process.wait()return process.pid@celery.task(bind=True)
def lora_train_task(self, json_test_demo):# 将json_test保存到临时文件中json_test_path = f"training_config_{json_test_demo['uid']}.json"json_test_path = os.path.abspath(json_test_path)with open(json_test_path, 'w') as f:json.dump(json_test_demo, f)# 使用多进程调用独立脚本process = multiprocessing.Process(target=run_script, args=(json_test_path, json_test_demo['uid']))process.start()process.join()return 0def terminate_process_tree(pid):try:parent = psutil.Process(pid)for child in parent.children(recursive=True):  # 递归找到所有子进程child.terminate()parent.terminate()except psutil.NoSuchProcess:pass# 示例:终止任务时调用终止子进程函数
celery.control.revoke(args['task_id'], terminate=True, signal='SIGKILL')
if args['pid_id']:terminate_process_tree(int(args['pid_id']))

结论

本文示范了如何通过混合使用Celery、Multiprocessing与Subprocess来处理复杂的任务执行场景,同时介绍了通过psutil库来正确管理和终止子进程。这种方法能够确保系统资源的合理使用,并避免出现僵尸进程问题。希望本文对你在实际项目中处理类似问题时有所帮助。

http://www.lryc.cn/news/499019.html

相关文章:

  • Django模板系统
  • 15. 文件操作
  • 清风数学建模学习笔记——Topsis法
  • 组合总和习题分析
  • 基于eFramework车控车设中间件介绍
  • L17.【LeetCode笔记】另一棵树的子树
  • BGP通过route-policy路由策略调用ip-prefix网络前缀实现负载均衡与可靠性之AS-path属性
  • 每日速记10道java面试题14-MySQL篇
  • 内存图及其画法
  • Ansys Maxwell:Qi 无线充电组件
  • 【Shell 脚本实现 HTTP 请求的接收、解析、处理逻辑】
  • 【北京迅为】iTOP-4412全能版使用手册-第六十七章 USB鼠标驱动详解
  • 【青牛科技】拥有两个独立的、高增益、内部相位补偿的双运算放大器,可适用于单电源或双电源工作——D4558
  • Kafka 数据写入问题
  • 实战ansible-playbook(九)-profile配置- 确保 CUDA 和 MPI 环境变量正确设置并立即生效
  • 气膜馆:科技与环保融合的未来建筑新选择—轻空间
  • git回退到某个版本git checkout和git reset命令的区别
  • Preprocess
  • stm32 spi接口传输asm330l速率优化(及cpu和dma方式对比)
  • 数字时代的文化宝库:存储技术与精神生活
  • flex: 1 display:flex 导致的宽度失效问题
  • Hive 窗口函数与分析函数深度解析:开启大数据分析的新维度
  • 前端工程 Node 版本如何选择
  • 推荐在线Sql运行
  • 【数据结构】【线性表】特殊的线性表-字符串
  • app-1 App 逆向环境准备(mumu模拟器+magisk+LSPosed+算法助手+抓包(socksDroid+charles)+Frida环境搭建
  • 在米尔FPGA开发板上实现Tiny YOLO V4,助力AIoT应用
  • 【IT】测试用例模版(含示例)
  • react dnd——一个拖拽组件
  • 3GPP R18 LTM(L1/L2 Triggered Mobility)是什么鬼?(三) RACH-less LTM cell switch