通过AirFlow远程调度TensorFlow机器学习程序

本文介绍了如何使用AirFlow远程调度TensorFlow的机器学习程序,以解决长时间运行的问题。在Prism框架中,通过浏览器端的SAPUI5应用调用AirFlow,AirFlow再触发TensorFlow程序,数据来源于Zabbix,结果同样写回Zabbix。
摘要由CSDN通过智能技术生成

通过AirFlow远程调度TensorFlow机器学习程序

TensorFlow机器学习程序运行时间比较长,因此调度TensorFlow机器学习程序需要考虑采用异步而不是同步调用的方式。我们开发的机器学习应用框架Prism中,我们通过浏览器端调用AirFlow,AirFlow调用TensorFlow机器学习程序的方法,实现了远程调用TensorFlow机器学习程序。TensorFlow机器学习程序所需要的输入数据来自Zabbix,处理结果写入Zabbix。

1、运行环境

  • 服务器操作系统:Linux i-cbp9w1nr 4.4.0-116-generic #140-Ubuntu SMP Mon Feb 12 21:23:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • TensorFlow:v1.7.0
  • AirFlow:v1.9.0
  • Airflow REST API Plugin:最新版本

2、编写AirFlow DAG 

输入一下命令,查看DAG文件:

source ~/tensorflow/bin/activate
cd ~/airflow/dags
vi dag_tfts_ar_tep_zabbix_r4.py

dag_tfts_ar_tep_zabbix_r4.py 原代码如下:

# -*- coding: utf-8 -*-
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# -*- coding: utf-8 -*-
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from datetime import timedelta
import airflow
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.python_operator import PythonOperator
from pprint import pprint

dag = DAG("dag_tfts_ar_tep_zabbix_r4",
          default_args={"owner": "prism",
                        "start_date": airflow.utils.dates.days_ago(1)},
          schedule_interval='@once',
          dagrun_timeout=timedelta(minutes=4)
          )

my_templated_command = """
    echo "{
  {ts}}" >>/tmp/predix/testoutput.txt
    echo "dag_id: dag_tfts_ar_tep_zabbix_r4" >>/tmp/predix/testoutput.txt
    echo "task_id: task_tfts_ar_tep_zabbix_r4" >>/tmp/predix/testoutput.txt
    echo " 'cfg was passed in via Airflow CLI REST API (trigger_dag) with value {
  { dag_run.conf.get(\'cfg\') }} " >>/tmp/predix/testoutput.txt
    echo " 'miff was passed in via BashOperator with value {
  { params.miff }} " >>/tmp/predix/testoutput.txt
    /home/ubuntu/tfts_zabbix/tfts_ar_tep_zabbix.py --cfg="{
  { dag_run.conf.get(\'cfg\') }}"
"""

run_this = BashOperator(
    task_id='task_tfts_ar_tep_zabbix_r4',
    bash_command=my_templated_command,
    params={"miff":"agg"},
    dag=dag)
                    
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值