PrefectHQ / prefect

Prefect is a workflow orchestration framework for building resilient data pipelines in Python.
https://prefect.io
Apache License 2.0
16.16k stars 1.58k forks source link

Prefect deploy hangs indefinitely when flow imports AzureML MLClient and MlflowClient #15621

Open AMontgomerie opened 1 week ago

AMontgomerie commented 1 week ago

Bug summary

After upgrading from prefect 2 to 3, running prefect --no-prompt deploy --prefect-file prefect.yaml --all gets stuck indefinitely. With log level set to debug, the only console output is:

❯ prefect --no-prompt deploy --prefect-file prefect.yaml --all
07:52:51.394 | DEBUG   | prefect.profiles - Using profile 'local'
07:52:51.488 | DEBUG   | prefect.client - Connecting to API at http://127.0.0.1:4200/api/
07:52:51.522 | DEBUG   | prefect.client - Connecting to API at http://127.0.0.1:4200/api/
07:52:51.557 | DEBUG   | prefect.client - Connecting to API at http://127.0.0.1:4200/api/

After lots of trial and error, I managed to track down the lines of code that are causing it to get stuck. If my flow imports both azure.ai.ml.MLClient and mlflow.tracking.MlflowClient then this issue occurs. If either class is not imported then deployment goes ahead normally.

For example:

# hello.py
from prefect import flow
from prefect.logging import get_run_logger

from azure.ai.ml import MLClient  # noqa
from mlflow.tracking import MlflowClient  # noqa

@flow
def hello():
    get_run_logger().info("hello")

if __name__ == "__main__":
    hello()
name: test-project
prefect-version: 3.0.5

deployments:
  - name: development
    tags: null
    schedule: null
    entrypoint: hello.py:hello
    work_pool:
      name: my-work-pool
      job_variables:
        image: myimage:latest

Version info (prefect version output)

❯ prefect version
07:59:09.542 | DEBUG   | prefect.profiles - Using profile 'local'
Version:             3.0.5
API version:         0.8.4
Python version:      3.11.9
Git commit:          1d1b92a7
Built:               Tue, Oct 8, 2024 5:55 PM
OS/Arch:             linux/x86_64
Profile:             local
Server type:         server
Pydantic version:    2.7.1
Integrations:
  prefect-kubernetes: 0.5.0

Additional context

Other dependencies:

I have a local self-hosted Prefect server running inside Docker Desktop Kubernetes, and also a Prefect Cloud account with a work pool that connects to an AKS cluster, and the same issue occurs when attempting to deploy to either.

zzstoatzz commented 1 week ago

hi @AMontgomerie - hmm interesting. I have reproduced but I'm not sure what's causing this to hang

I will say that you should be able to unblock yourself by deferring your imports, something like

# hello.py

from prefect import flow
from prefect.logging import get_run_logger

@flow
def hello():
    from azure.ai.ml import MLClient  # noqa
    from mlflow.tracking import MlflowClient  # noqa

    get_run_logger().info("hello")

if __name__ == "__main__":
    hello()

will try to understand more about what's going on

AMontgomerie commented 1 week ago

Thanks for the workaround! It was a little awkward to implement because these imports are inside a utility package that is imported throughout a lot of different flows, but I am able to deploy the flows now at least.