josh-fell commented on a change in pull request #16866:
URL: https://github.com/apache/airflow/pull/16866#discussion_r684260804
##########
File path: airflow/example_dags/example_xcom.py
##########
@@ -18,50 +18,61 @@
"""Example DAG demonstrating the usage of XComs."""
from airflow import DAG
-from airflow.operators.python import PythonOperator
+from airflow.operators.python import PythonOperator, get_current_context
from airflow.utils.dates import days_ago
value_1 = [1, 2, 3]
value_2 = {'a': 'b'}
+value_3 = 'some value'
def push(**kwargs):
"""Pushes an XCom without a specific target"""
+
+ # Using the "classic" approach
kwargs['ti'].xcom_push(key='value from pusher 1', value=value_1)
+ # Using the approach available as of Airflow 2.0. When using this
approach, `**kwargs`
+ # is not needed to access the execution context.
+ context = get_current_context()
+ context['ti'].xcom_push(key='other value from pusher 1', value=value_2)
Review comment:
I think having an another example using the `get_current_context()`
function outside of the [Concepts
documentation](https://airflow.apache.org/docs/apache-airflow/2.0.0/concepts.html#accessing-current-context)
could be beneficial for those who really like using task-decorated functions.
Maybe rewriting the example that more closely aligns with the concept docs plus
some detailed comments could be acceptable. WDYT?
I do also agree that if there is a doubt about displaying an example we
shouldn't even bother given all the blind copy/paste happening. Certainly some
other updates could be made to examples which border on anti-patterns (e.g.
`days_ago()` in `start_date`). I feel the rewrite would alleviate doubt here
though.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]