potiuk commented on code in PR #33355:
URL: https://github.com/apache/airflow/pull/33355#discussion_r1292876578


##########
airflow/operators/python.py:
##########
@@ -606,7 +613,31 @@ def _prepare_venv(self, venv_path: Path) -> None:
             index_urls=self.index_urls,
         )
 
+    def _ensure_venv_cache_exists(self, venv_cache_path: Path) -> Path:
+        """Helper to ensure a valid venv is set up and will create inplace."""
+        hash_object = 
hashlib_wrapper.md5(",".join(self._requirements_list()).encode())
+        requirements_hash = hash_object.hexdigest()
+        venv_path = venv_cache_path / f"venv-{requirements_hash[0:8]}"
+        self.log.info("Python Virtualenv will be cached in %s", venv_path)
+        venv_path.parent.mkdir(parents=True, exist_ok=True)
+        with open(f"{venv_path}.lock", "w") as f:
+            # Ensure that cache is not build by parallel workers
+            fcntl.flock(f, fcntl.LOCK_EX)
+
+            if venv_path.exists():
+                self.log.info("Re-using cached Python Virtualenv in %s", 
venv_path)
+            else:
+                venv_path.mkdir(parents=True)
+                self._prepare_venv(venv_path)

Review Comment:
   try/except needs to be handled here. If there is any exception during 
creation of the venv, it should be entirely deleted.



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