目录
使用场景
数据湖 = 分布式存储 + 蓝光库(Panasonic, Sony)
最近在做数据湖项目过程中,遇到一个这样的需求,需要实时更新任务进度给上层应用系统,从之前对蓝光库原生的api了解,并没有这样的API可以实时拿到它的进度,目前文件从共享存储到蓝光库盘匣应该大多都是通过开源的刻盘工具来进行的(growisofs),可能每个厂商都会在此基础之上进行修改,那目前为止核心的问题就是如何通过python实时获取growisofs的所有输出
解决方案
- 环境介绍
growisofs -- v7.1 python2 -- v2.7 python3 -- v3.5+
-
直接上代码,这里我是将growifsofs所有输出重定向到一个文件
cmd_str = 'growisofs -Z ' + drive_name + ' -use-the-force-luke=spare:none -iso-level 4 ' + filepath
back_message = subprocess.Popen(cmd_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
next_line = back_message.stdout.readline()
if stdout_file:
self.record_cmd_stdout_info(stdout_file, next_line)
if GROW_MES in next_line:
logger.info("growisofs_status = {}".format(next_line))
if SUCCESS_STATUS in next_line:
errno_code = True
elif SUCCESS_END in next_line:
logger.info("growisofs_end = {}".format(next_line))
grow_status = True
if next_line == "" and back_message.poll() != None:
break
# 需要关注的点是需要将stderr重定向为STDOUT, 这样就可以拿到growisofs的所有输出了
结果
本次任务实时进度是通过检索文件里的内容定时更新给上层应用
Thanks~