You need to enable JavaScript to run this app. GCP Dataflow配置requirements_file后Worker进程停滞的问题与解决方案
优惠活动
大模型
产品
解决方案
定价
更多

GCP Dataflow配置requirements_file后Worker进程停滞的问题与解决方案

GCP Dataflow Python 任务依赖加载异常记录

触发场景

需要在GCP Dataflow环境中使用mecab-python等带外部依赖的Python库时,常规操作是通过requirements.txt文件声明依赖列表,运行任务时搭配--requirements_file参数让Worker节点自动安装依赖。本次测试即便严格按照规范编写requirements.txt,Worker节点始终无法进入实际数据处理阶段。
最初的任务执行命令如下:

python3 -m apache_beam.examples.wordcount \
  --region $REGION \
  --output gs://$BUCKET/results/outputs \
  --runner  DataflowRunner \
  --project $PROJECT \
  --temp_location gs://$BUCKET/tmp/ \
  --requirements_file  ./requirements.txt

异常表现

携带上述参数运行时,Worker输出以下日志后就停止推进,无后续数据处理动作:

INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:02:51.397Z: JOB_MESSAGE_DEBUG: Value "GroupAndSum/GroupByKey/Session" materialized.
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:02:51.418Z: JOB_MESSAGE_DEBUG: Value "Write/Write/WriteImpl/GroupByKey/Session" materialized.
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:02:51.429Z: JOB_MESSAGE_BASIC: Executing operation Read/Read+Split+PairWIthOne+GroupAndSum/GroupByKey+GroupAndSum/Combine/Partial+GroupAndSum/GroupByKey/Reify+GroupAndSum/GroupByKey/Write
INFO:apache_beam.runners.dataflow.dataflow_runner:Job 2021-01-17_05_02_36-17241324845585998354 is in state JOB_STATE_RUNNING
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:03:17.082Z: JOB_MESSAGE_DETAILED: Autoscaling: Raised the number of workers to 1 based on the rate of progress in the currently running stage(s).
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:03:46.622Z: JOB_MESSAGE_DETAILED: Workers have started successfully.
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:03:46.632Z: JOB_MESSAGE_DETAILED: Workers have started successfully.

对照测试发现,只要移除--requirements_file ./requirements.txt参数,Worker就能正常跑完整个任务,正常推进时的日志如下:

INFO:apache_beam.runners.dataflow.dataflow_runner:Job 2021-01-17_05_25_22-15232043370416313688 is in state JOB_STATE_RUNNING
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:25:58.407Z: JOB_MESSAGE_DETAILED: Autoscaling: Raised the number of workers to 1 based on the rate of progress in the currently running stage(s).
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:26:29.645Z: JOB_MESSAGE_DETAILED: Workers have started successfully.
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:26:29.666Z: JOB_MESSAGE_DETAILED: Workers have started successfully.
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:28:43.364Z: JOB_MESSAGE_BASIC: Finished operation Read/Read+Split+PairWIthOne+GroupAndSum/GroupByKey+GroupAndSum/Combine/Partial+GroupAndSum/GroupByKey/Reify+GroupAndSum/GroupByKey/Write <<Proceed to this stage.
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:28:43.390Z: JOB_MESSAGE_BASIC: Executing operation GroupAndSum/GroupByKey/Close
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:28:43.416Z: JOB_MESSAGE_BASIC: Finished operation GroupAndSum/GroupByKey/Close
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:28:51.145Z: JOB_MESSAGE_BASIC: Finished operation Write/Write/WriteImpl/DoOnce/Read+Write/Write/WriteImpl/InitializeWrite
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:28:51.167Z: JOB_MESSAGE_DEBUG: Value "Write/Write/WriteImpl/DoOnce/Read.out" materialized.
INFO:apache_beam.runners.dataflow.dataflow_runner:2021-01-17T13:28:51.177Z: JOB_MESSAGE_DEBUG: Value "Write/Write/WriteImpl/InitializeWrite.out" materialized.

测试环境信息

  • 使用的Apache Beam版本为apache_beam-2.27.0-cp37-cp37m-manylinux1_x86_64.whl,对应Python3.7运行环境
  • 出问题时使用的requirements.txt仅声明了一个无系统编译依赖的包python-dotenv,可排除依赖本身安装失败导致的卡点

最终解决方案

改用setup.py文件声明项目依赖,运行时搭配--setup_file参数指定该文件路径后,任务可正常全流程执行。

  • 示例setup.py内容:
# setup.py
from setuptools import setup, find_packages
setup(
    name='foobar test',
    version='0.0.1',
    install_requires=[
        'python-dotenv==0.15.0',
    ],
    description='foobazz description',
)
  • 调整后的任务执行命令:
#run.sh
python3 -m apache_beam.examples.wordcount \
  --region $REGION \
  --output gs://$BUCKET/results/outputs \
  --runner  DataflowRunner \
  --project $PROJECT \
  --temp_location gs://$BUCKET/tmp/ \
   --setup_file ./setup.py  

内容的提问来源于stack exchange,提问作者芝尾幸一郎

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.09 04:06:03