o-nikolas commented on code in PR #25780:
URL: https://github.com/apache/airflow/pull/25780#discussion_r949425679


##########
airflow/example_dags/example_python_operator.py:
##########
@@ -93,3 +93,28 @@ def callable_virtualenv():
 
         virtualenv_task = callable_virtualenv()
         # [END howto_operator_python_venv]
+
+        # [START howto_operator_external_python]
+        @task.external_python(task_id="virtualenv_python", 
python="/ven/bin/python")

Review Comment:
   Super-minor-nit: s/ven/venv/g



##########
airflow/example_dags/example_python_operator.py:
##########
@@ -93,3 +93,28 @@ def callable_virtualenv():
 
         virtualenv_task = callable_virtualenv()
         # [END howto_operator_python_venv]
+
+        # [START howto_operator_external_python]
+        @task.external_python(task_id="virtualenv_python", 
python="/ven/bin/python")
+        def callable_external_python():
+            """
+            Example function that will be performed in a virtual environment.
+
+            Importing at the module level ensures that it will not attempt to 
import the
+            library before it is installed.
+            """
+            from time import sleep
+
+            from colorama import Back, Fore, Style
+
+            print(Fore.RED + 'some red text')
+            print(Back.GREEN + 'and with a green background')
+            print(Style.DIM + 'and in dim text')
+            print(Style.RESET_ALL)
+            for _ in range(10):
+                print(Style.DIM + 'Please wait...', flush=True)
+                sleep(10)

Review Comment:
   I know this is just copy/pasted from above, but 100s seems very long for a 
task in a simple example dag.
   
   (Also speaking of copy/paste, maybe extract this into a helper so the code 
isn't duplicated in both tasks).



##########
docs/apache-airflow/tutorial_taskflow_api.rst:
##########
@@ -253,17 +253,21 @@ It is worth noting that the Python source code (extracted 
from the decorated fun
     You should upgrade to Airflow 2.2 or above in order to use it.
 
 If you don't want to run your image on a Docker environment, and instead want 
to create a separate virtual
-environment on the same machine, you can use the ``@task.virtualenv`` 
decorator instead. The ``@task.virtualenv``
-decorator will allow you to create a new virtualenv with custom libraries and 
even a different
-Python version to run your function.
+environment on the same machine, you can use the ``@task.virtualenv`` 
decorator or ``@task.external_python``
+instead. The ``@task.virtualenv`` decorator will allow you to create a new 
virtualenv with custom libraries
+and even a different Python version to run your function, similarly 
``@task.external_python`` will allow you
+to run Airflow task in pre-defined, immutable virtualenv (which also could 
have different set of custom

Review Comment:
   ```suggestion
   to run an Airflow task in pre-defined, immutable virtualenv (which also 
could have a different set of custom
   ```



##########
docs/apache-airflow/howto/operator/python.rst:
##########
@@ -89,6 +89,36 @@ If additional parameters for package installation are needed 
pass them in ``requ
 All supported options are listed in the `requirements file format 
<https://pip.pypa.io/en/stable/reference/requirements-file-format/#supported-options>`_.
 
 
+.. _howto/operator:ExternalPythonOperator:
+
+ExternalPythonOperator
+======================
+
+The ExternalPythonOperator can help you to run some of your tasks with 
different set of Python

Review Comment:
   ```suggestion
   The ExternalPythonOperator can help you to run some of your tasks with a 
different set of Python
   ```



##########
docs/apache-airflow/howto/operator/python.rst:
##########
@@ -89,6 +89,36 @@ If additional parameters for package installation are needed 
pass them in ``requ
 All supported options are listed in the `requirements file format 
<https://pip.pypa.io/en/stable/reference/requirements-file-format/#supported-options>`_.
 
 
+.. _howto/operator:ExternalPythonOperator:
+
+ExternalPythonOperator
+======================
+
+The ExternalPythonOperator can help you to run some of your tasks with 
different set of Python
+libraries than other tasks (and than the main Airflow environment).
+
+Use the :class:`~airflow.operators.python.ExternalPythonOperator` to execute 
Python callables inside a
+pre-defined virtual environment. The virtualenv should be preinstalled in the 
environment where
+Python is run and in case ``dill`` is used, it has to be preinstalled in the 
virtualenv (the same
+version that is installed in main Airflow environment).
+
+.. exampleinclude:: /../../airflow/example_dags/example_python_operator.py
+    :language: python
+    :dedent: 4
+    :start-after: [START howto_operator_external_python]
+    :end-before: [END howto_operator_external_python]
+
+Passing in arguments
+^^^^^^^^^^^^^^^^^^^^
+
+You can use the ``op_args`` and ``op_kwargs`` arguments the same way you use 
it in the PythonOperator.

Review Comment:
   ```suggestion
   You can use the ``op_args`` and ``op_kwargs`` arguments the same way you use 
them in the PythonOperator.
   ```



-- 
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]

Reply via email to