-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Fix DockerOperator xcom #9464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Fix DockerOperator xcom #9464
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
airflow/providers/docker/example_dags/example_docker_xcom.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # pylint: disable=missing-function-docstring | ||
| """ | ||
| This sample writes and pull from xcom using DockerOperator. | ||
| The following operators are being used: DockerOperator & | ||
| BashOperator. | ||
| """ | ||
| from datetime import timedelta | ||
|
|
||
| from airflow import DAG | ||
| from airflow.operators.bash_operator import BashOperator | ||
| from airflow.operators.docker_operator import DockerOperator | ||
| from airflow.utils.dates import days_ago | ||
|
|
||
| with DAG( | ||
| dag_id="example_docker_xcom", | ||
| schedule_interval="@daily", | ||
| start_date=days_ago(5), | ||
| catchup=True, | ||
| max_active_runs=2, | ||
| default_args={ | ||
| "owner": "airflow", | ||
| "retries": 2, | ||
| "retry_delay": timedelta(minutes=5), | ||
| }, | ||
| ) as dag: | ||
|
|
||
| write_xcom_last = DockerOperator( | ||
| task_id="write_xcom_last", | ||
| image="python:3-slim", | ||
| api_version="auto", | ||
| command="""python -c "import logging;[ | ||
| print(i) for i in range(10)];logging.warning('this is a warning')" """, | ||
| do_xcom_push=True, | ||
| xcom_all=False, | ||
| auto_remove=True, | ||
| network_mode="bridge", | ||
| ) | ||
|
|
||
| write_xcom_all = DockerOperator( | ||
| task_id="write_xcom_all", | ||
| image="python:3-slim", | ||
| api_version="auto", | ||
| command="""python -c "import logging;[ | ||
| print(i) for i in range(10)];logging.warning('this is a warning')" """, | ||
| do_xcom_push=True, | ||
| xcom_all=True, | ||
| auto_remove=True, | ||
| network_mode="bridge", | ||
| ) | ||
|
|
||
| pull_xcom_last = DockerOperator( | ||
| task_id="pull_xcom", | ||
| image="ubuntu:20.04", | ||
| api_version="auto", | ||
| command="""echo {{ ti.xcom_pull(task_ids="write_xcom_last") }}""", | ||
| do_xcom_push=True, | ||
| xcom_all=False, | ||
| auto_remove=True, | ||
| network_mode="bridge", | ||
| ) | ||
|
|
||
| pull_xcom_all = BashOperator( | ||
| task_id="pull_xcom_all", | ||
| bash_command="""echo '{{ ti.xcom_pull(task_ids="write_xcom_all") }}' """, | ||
| do_xcom_push=True, | ||
| ) | ||
|
|
||
| write_xcom_last >> pull_xcom_last | ||
| write_xcom_all >> pull_xcom_all |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi
I don't think using a generator instead of a list here solves the memory issue. The data will in the end be kept in memory - no matter you use a list or a generator.
I did a small test to verify this; I ran the following code in a Python3 terminal:
where
yes.logwas a 650 MB file.As soon as the execution of this code-block completed, I saw the memory used by this process increase by 650 MB. It makes sense as well because where else would generators be storing these logs if not in memory.
I am thinking that you might be able to achieve what you're trying to do by streaming the logs and using
yield.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback Akki. I'll make some tests during this week and see if I can solve this using yield and streaming.