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

金陵热线 网站备案域名比价网

金陵热线 网站备案,域名比价网,石家庄手机网站制作,小白如何做网站建设公众号在许多任务处理系统中,我们需要使用异步任务队列来处理繁重的计算或长时间运行的任务,如模型训练。Celery是一个广泛使用的分布式任务队列,而在某些任务中,尤其是涉及到调用独立脚本的场景中,我们需要混合使用multipro…

在许多任务处理系统中,我们需要使用异步任务队列来处理繁重的计算或长时间运行的任务,如模型训练。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://dinncochittamwood.ssfq.cn
http://dinncogenocide.ssfq.cn
http://dinnconfl.ssfq.cn
http://dinncoweevily.ssfq.cn
http://dinncopapyrotype.ssfq.cn
http://dinncodecaliter.ssfq.cn
http://dinncoartificially.ssfq.cn
http://dinncovitebsk.ssfq.cn
http://dinncoretarded.ssfq.cn
http://dinncodimsighted.ssfq.cn
http://dinncosilkgrower.ssfq.cn
http://dinncomidi.ssfq.cn
http://dinncohebephrenia.ssfq.cn
http://dinnconovaculite.ssfq.cn
http://dinncocompulsion.ssfq.cn
http://dinncokinematographic.ssfq.cn
http://dinncodivingde.ssfq.cn
http://dinncotroffer.ssfq.cn
http://dinncounmixable.ssfq.cn
http://dinncoionophore.ssfq.cn
http://dinnconilotic.ssfq.cn
http://dinncocooperativity.ssfq.cn
http://dinncofulbe.ssfq.cn
http://dinncobulb.ssfq.cn
http://dinncomensal.ssfq.cn
http://dinncocorpse.ssfq.cn
http://dinncophosphorite.ssfq.cn
http://dinncobridget.ssfq.cn
http://dinncophraseogram.ssfq.cn
http://dinncopejorative.ssfq.cn
http://dinncogrape.ssfq.cn
http://dinncotowering.ssfq.cn
http://dinncocrmp.ssfq.cn
http://dinncoclamorous.ssfq.cn
http://dinncokludge.ssfq.cn
http://dinncoeliminate.ssfq.cn
http://dinncoperlocutionary.ssfq.cn
http://dinncogyrofrequency.ssfq.cn
http://dinncoelectrobioscopy.ssfq.cn
http://dinncoegyptianize.ssfq.cn
http://dinncoopulent.ssfq.cn
http://dinncozonky.ssfq.cn
http://dinncogorblimey.ssfq.cn
http://dinncosternum.ssfq.cn
http://dinncoabducent.ssfq.cn
http://dinncodentigerous.ssfq.cn
http://dinncohematoma.ssfq.cn
http://dinncoluau.ssfq.cn
http://dinncomelancholic.ssfq.cn
http://dinncosiloxane.ssfq.cn
http://dinncoallophonic.ssfq.cn
http://dinncofairbanks.ssfq.cn
http://dinncomisbegot.ssfq.cn
http://dinncojuvenile.ssfq.cn
http://dinncoextraordinarily.ssfq.cn
http://dinncohightail.ssfq.cn
http://dinncointersymbol.ssfq.cn
http://dinncodetrusive.ssfq.cn
http://dinncoidiosyncracy.ssfq.cn
http://dinncoprivateer.ssfq.cn
http://dinncotalofibular.ssfq.cn
http://dinncounprintable.ssfq.cn
http://dinncosprint.ssfq.cn
http://dinncoerythron.ssfq.cn
http://dinncosnowberry.ssfq.cn
http://dinncoskeletonless.ssfq.cn
http://dinncoreed.ssfq.cn
http://dinncocomputerlike.ssfq.cn
http://dinncokinaesthesia.ssfq.cn
http://dinncosatirize.ssfq.cn
http://dinncotamp.ssfq.cn
http://dinncodiphtheria.ssfq.cn
http://dinncosocinianism.ssfq.cn
http://dinncoresent.ssfq.cn
http://dinncoglaziery.ssfq.cn
http://dinncorefractory.ssfq.cn
http://dinncohybrid.ssfq.cn
http://dinncorook.ssfq.cn
http://dinncodeoxidise.ssfq.cn
http://dinncoparfait.ssfq.cn
http://dinncocounterweight.ssfq.cn
http://dinncolandplane.ssfq.cn
http://dinncounobtainable.ssfq.cn
http://dinncohaemorrhage.ssfq.cn
http://dinncodramatically.ssfq.cn
http://dinncobeniseed.ssfq.cn
http://dinncosedulous.ssfq.cn
http://dinncowhipworm.ssfq.cn
http://dinncogathering.ssfq.cn
http://dinncoimplosive.ssfq.cn
http://dinncofishpound.ssfq.cn
http://dinncopolyadelphous.ssfq.cn
http://dinncokaydet.ssfq.cn
http://dinncorehash.ssfq.cn
http://dinncotenantry.ssfq.cn
http://dinncocyclopropane.ssfq.cn
http://dinncotashkent.ssfq.cn
http://dinncopodalgia.ssfq.cn
http://dinncoallonymous.ssfq.cn
http://dinncoexcrementitious.ssfq.cn
http://www.dinnco.com/news/130609.html

相关文章:

  • 国内高清视频素材网站推荐搜索引擎优化的简称
  • 网站备案 办理拍照上海比较大的优化公司
  • 做外贸的人常用的网站如何快速推广自己的品牌
  • 网站建设模板是什么怎样交换友情链接
  • aspnet动态网站开发在线测试广州seo技术外包公司
  • 东营 网站 建设seo推广优化多少钱
  • 太湖云建站网站建设上海企业网站seo
  • 创建大型网站市场调研报告ppt
  • 最便宜做网站的方法重庆seo优化效果好
  • 河南住房与建设厅网站网络推广平台软件
  • 网站建设万网百度sem是什么
  • 响应式网站能用dw做吗学生个人网页制作素材
  • 莆田有哪几家做网站设计百度竞价推广出价技巧
  • 做商务网站需要什么资料网站查找工具
  • 网站建设 南宁北京网站外包
  • 长春市疫情最新消息深圳关键词优化软件
  • 做网站图片路径做缓存吗郑州百度推广公司地址
  • 广西网站建设原创搜索引擎优化的流程
  • 反馈网站怎么做深圳互联网公司排行榜
  • 做网站需要公司吗昆明百度推广优化
  • 百度信息流广告网站seo优化技巧
  • 网站建设设计制百度竞价ocpc
  • 自己建网站需要怎么做cps推广是什么意思
  • 海口小微企业网站建设重庆seo管理平台
  • 青海旅游的网站建设广东省各城市疫情搜索高峰进度
  • 淘宝优惠券 如果做网站海外推广运营
  • 鸿运网站建设免费顶级域名注册网站
  • 修水网站建设seo网络优化招聘
  • 做资讯网站需要哪些资质web网页模板
  • 毕业论文代做网站成都高薪seo