WeiZhong94 commented on a change in pull request #10017: [FLINK-14019][python]
add support for managing environment and dependencies of Python UDF in Flink
Python API
URL: https://github.com/apache/flink/pull/10017#discussion_r349039373
##########
File path: flink-python/pyflink/fn_execution/boot.py
##########
@@ -45,105 +46,195 @@
from google.protobuf import json_format, text_format
-parser = argparse.ArgumentParser()
-
-parser.add_argument("--id", default="", help="Local identifier (required).")
-parser.add_argument("--logging_endpoint", default="",
- help="Logging endpoint (required).")
-parser.add_argument("--artifact_endpoint", default="",
- help="Artifact endpoint (required).")
-parser.add_argument("--provision_endpoint", default="",
- help="Provision endpoint (required).")
-parser.add_argument("--control_endpoint", default="",
- help="Control endpoint (required).")
-parser.add_argument("--semi_persist_dir", default="/tmp",
- help="Local semi-persistent directory (optional).")
-
-args = parser.parse_args()
-
-worker_id = args.id
-logging_endpoint = args.logging_endpoint
-artifact_endpoint = args.artifact_endpoint
-provision_endpoint = args.provision_endpoint
-control_endpoint = args.control_endpoint
-semi_persist_dir = args.semi_persist_dir
-
def check_not_empty(check_str, error_message):
if check_str == "":
logging.fatal(error_message)
exit(1)
-check_not_empty(worker_id, "No id provided.")
-check_not_empty(logging_endpoint, "No logging endpoint provided.")
-check_not_empty(artifact_endpoint, "No artifact endpoint provided.")
-check_not_empty(provision_endpoint, "No provision endpoint provided.")
-check_not_empty(control_endpoint, "No control endpoint provided.")
-
-logging.info("Initializing python harness: %s" % " ".join(sys.argv))
-
-metadata = [("worker_id", worker_id)]
-
-# read job information from provision stub
-with grpc.insecure_channel(provision_endpoint) as channel:
- client = ProvisionServiceStub(channel=channel)
- info = client.GetProvisionInfo(GetProvisionInfoRequest(),
metadata=metadata).info
- options = json_format.MessageToJson(info.pipeline_options)
-
-staged_dir = os.path.join(semi_persist_dir, "staged")
-
-# download files
-with grpc.insecure_channel(artifact_endpoint) as channel:
- client = ArtifactRetrievalServiceStub(channel=channel)
- # get file list via retrieval token
- response =
client.GetManifest(GetManifestRequest(retrieval_token=info.retrieval_token),
- metadata=metadata)
- artifacts = response.manifest.artifact
- # download files and check hash values
- for artifact in artifacts:
- name = artifact.name
- permissions = artifact.permissions
- sha256 = artifact.sha256
- file_path = os.path.join(staged_dir, name)
- if os.path.exists(file_path):
- with open(file_path, "rb") as f:
+python_exec = sys.executable
+
+PYTHON_REQUIREMENTS_FILE = "_PYTHON_REQUIREMENTS_FILE"
+PYTHON_REQUIREMENTS_CACHE = "_PYTHON_REQUIREMENTS_CACHE"
+PYTHON_REQUIREMENTS_DIR_ENV = "_PYTHON_REQUIREMENTS_INSTALL_DIR"
+
+
+def pip_install_requirements_process_mode():
+ if (PYTHON_REQUIREMENTS_FILE in os.environ
+ and PYTHON_REQUIREMENTS_DIR_ENV in os.environ):
+ requirements_file_path = os.environ[PYTHON_REQUIREMENTS_FILE]
+ requirements_target_path = os.environ[PYTHON_REQUIREMENTS_DIR_ENV]
+ requirements_dir_path = None
+
+ if PYTHON_REQUIREMENTS_CACHE in os.environ:
+ requirements_dir_path = os.environ[PYTHON_REQUIREMENTS_CACHE]
+
+ if "PATH" in os.environ:
+ os.environ["PATH"] = \
+ os.pathsep.join(
+ [os.path.join(requirements_target_path, "bin"),
os.environ["PATH"]])
+ else:
+ os.environ["PATH"] = os.path.join(requirements_target_path, "bin")
+ env = dict(os.environ)
+ from distutils.dist import Distribution
+ install_obj = Distribution().get_command_obj('install', create=True)
+ install_obj.prefix = requirements_target_path
+ install_obj.finalize_options()
+ installed_dir = [install_obj.install_purelib]
+ if install_obj.install_purelib != install_obj.install_platlib:
+ installed_dir.append(install_obj.install_platlib)
+ if len(installed_dir) > 0:
+ installed_python_path = os.pathsep.join(installed_dir)
+ if "PYTHONPATH" in env:
+ env["PYTHONPATH"] = os.pathsep.join(
+ [installed_python_path, env["PYTHONPATH"]])
+ else:
+ env["PYTHONPATH"] = installed_python_path
+ # since '--prefix' option is only supported for pip 8.0+, so here we
fallback to
+ # use '--install-option' when the pip version is lower than 8.0.0.
+ from pkg_resources import get_distribution, parse_version
+ pip_version = get_distribution("pip").version
+ line_continuation = None
+ with open(requirements_file_path, "r") as f:
+ while True:
+ text_line = f.readline()
Review comment:
IMHO we should give users a chance to install and upgrade the pip itself or
other package-installation-related packages (e.g. wheel), and installing the
packages line by line allows users to do that.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services