Skip to main content
 首页 » 编程设计

python之如果安装的模块在 zip 文件的顶层添加 .py 文件,Airflow 打包的 DAG 将无法工作

2024年12月31日12虾米哥

我正在为一个数据科学家团队设置一个多用户 Airflow 集群,DAG 有多种用途(ETL、NLP、ML、NN...),其中一些具有特定的 Python 依赖性。 我不能简单地在系统级别添加所有 DAG 依赖项。当然,我可以为常见用途设置基线,但对于特定需求,依靠压缩的 DAG 功能会非常有帮助。

因此,为了解决多上下文问题,我正在测试 packaged DAG Airflow 1.9.0 的功能(在 Ubuntu 16.04 上)。

我正在按照示例使用任意 pypi 包对其进行测试。

  • 我随机选择了一个 python 模块 (python-crontab)。 (在此之前,我尝试使用更强大的模块,但重现测试需要更长的时间)
  • 测试场景:能够导入该模块并在压缩的 DAG 中打印其版本
  • 我是这样做的:

    $ virtualenv venv --python=python3 
    $ source venv/bin/activate 
    (venv) $ mkdir contents && cd contents 
    $ pip install --install-option="--install-lib=$PWD" python-crontab 
    $ cp ../my_dag.py . 
    $ zip -r ../test_zip_2.zip * 
    $ cp ../test_zip_2.zip /path/to/dags 
    $ journalctl -f -u airflow-scheduler.service 
    (...) 
    WARNING - No viable dags retrieved from /path/to/dags/test_zip_2.zip 
    
  • 我的 DAG 的内容:

    import crontab 
    import airflow.utils.dates as a_dates 
    from airflow.operators.python_operator import PythonOperator 
    from airflow.operators.dummy_operator import DummyOperator 
    from airflow.models import DAG 
     
    from pprint import pprint 
     
    args = { 
        'owner': 'airflow', 
        'start_date': a_dates.days_ago(1) 
    } 
     
    def print_context(ds, **kwargs): 
        pprint(kwargs) 
        print(ds) 
        print(crontab.__version__) 
        return 'Whatever you return gets printed in the logs' 
     
    with DAG(dag_id='test_zip', default_args=args, schedule_interval=None) as dag: 
        ( 
            PythonOperator( 
                task_id='print_the_context', 
                provide_context=True, 
                python_callable=print_context, 
            ) 
            >> DummyOperator( 
                task_id='do_nothing' 
            ) 
        ) 
    

检查后the code ,如果发现不包含单词“DAG”和“airflow”的 .py 文件,解析 ZIP 文件的逻辑似乎会立即退出。

问题是,我上面描述的方法实际上在存档的根目录下生成了其他 .py 文件。

$ ll 
total 100 
drwxr-xr-x 1 vagrant vagrant   442 Jun  1 14:48 ./ 
drwxr-xr-x 1 vagrant vagrant   306 Jun  1 15:30 ../ 
-rw-rw-r-- 1 vagrant vagrant  3904 Dec 30  2015 cronlog.py 
-rw-rw-r-- 1 vagrant vagrant 44651 May 25 16:44 crontab.py 
-rw-rw-r-- 1 vagrant vagrant  4438 Dec 28  2015 crontabs.py 
drwxr-xr-x 1 vagrant vagrant   476 Jun  1 14:26 dateutil/ 
-rw-r--r-- 1 vagrant vagrant  6148 Jun  1 14:24 .DS_Store 
drwxr-xr-x 1 vagrant vagrant   204 Jun  1 14:26 __pycache__/ 
drwxr-xr-x 1 vagrant vagrant   272 Jun  1 14:26 python_crontab-2.3.3-py3.5.egg-info/ 
drwxr-xr-x 1 vagrant vagrant   306 Jun  1 14:26 python_dateutil-2.7.3-py3.5.egg-info/ 
drwxr-xr-x 1 vagrant vagrant   238 Jun  1 14:26 six-1.11.0-py3.5.egg-info/ 
-rw-rw-r-- 1 vagrant vagrant 30888 Sep 17  2017 six.py 
-rw-r--r-- 1 vagrant vagrant   832 Jun  1 14:48 my_dag.py 

不过,我测试过的许多知名软件包都会生成这些顶级 .py 文件。例如。安装 scrapy、numpy、pandas 等产生了同样的困惑。

那么,我的选择是什么(不 fork Airflow ^_^)?

我是否正确理解了此功能?

感谢您的帮助!

请您参考如下方法:

对于那些现在阅读本文的人,应按照以下自 v1.10.3 开始的更新说明进行操作。

Note

When searching for DAGs, Airflow only considers python files that contain the strings "airflow" and "DAG" by default. To consider all python files instead, disable the DAG_DISCOVERY_SAFE_MODE configuration flag.

https://github.com/apache/airflow/blob/1.10.3/docs/concepts.rst#dags

https://github.com/apache/airflow/blob/1.10.3/UPDATING.md#new-dag_discovery_safe_mode-config-option-1