airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Add `execute_tasks_new_python_interpreter` argument to `PythonOperator`

Open pedro-cf opened this issue 2 years ago • 6 comments

Description

Currently from my knowledge it is only possible to force all python tasks to execute in a new interpreter for every execution globally via this setting:

  • https://airflow.apache.org/docs/apache-airflow/stable/configurations-ref.html#execute-tasks-new-python-interpreter

Would it be possible to add this argument to the PythonOperator so it is possible to control this behavior at the task level?

Use case/motivation

  • https://github.com/apache/airflow/discussions/40956

Related issues

  • https://github.com/apache/airflow/discussions/40956

Are you willing to submit a PR?

  • [ ] Yes I am willing to submit a PR!

Code of Conduct

pedro-cf avatar Jul 29 '24 09:07 pedro-cf

Marked it as a good first issue for anyone to take - but feel free to propose a PR as well.

potiuk avatar Jul 30 '24 03:07 potiuk

Hi @potiuk, I would like to work on this issue as my first issue. May I ?

Prab-27 avatar Jul 30 '24 09:07 Prab-27

Sure

potiuk avatar Jul 30 '24 09:07 potiuk

@potiuk I have updated description of PR and answer your que. I think it would solve your query.

Prab-27 avatar Sep 04 '24 20:09 Prab-27

@Prab-27 We are unassigning you from this issue as part of our updated assignment policy.

This is not meant to discourage your contribution — quite the opposite! You are still very welcome to work on this issue and submit a PR for it. Simply comment that you are working on it and open a PR when ready.

We found that formal assignments were not working well, as they often prevented others from contributing when the assignee was not actively working on the issue.

potiuk avatar Mar 02 '26 19:03 potiuk

Hi, I'd like to work on this. Looking at the current code, the exec decision is made in the task supervisor (task_process_uses_exec() in task-sdk/src/airflow/sdk/execution_time/supervisor.py), which today only reads the global [core] execute_tasks_new_python_interpreter config. My proposed approach:

  1. Add an execute_tasks_new_python_interpreter: bool | None = None argument to PythonOperator (None = fall back to the global config).
  2. Carry the value to the supervisor alongside the other per-task execution context (similar to how retry-related context is passed), so task_process_uses_exec() can prefer the per-task value when set.
  3. Keep the platform gate (macOS) forcing exec regardless.

Before I write code, could a maintainer confirm whether the per-task value should travel via the TI context from the API server, or whether there's a preferred existing channel for per-task execution flags? Happy to adjust the design.

dasarijashwanth avatar Nov 27 '26 06:11 dasarijashwanth