Remote Application Lifecycle
Managing the lifecycle of a remote application in flyte-sdk involves controlling its deployment state, monitoring its health, and utilizing temporary contexts for short-lived tasks. The App class in flyte.remote provides the primary interface for these operations, abstracting the underlying gRPC communication handled by the AppService.
Retrieving a Remote App
Before you can manage an application's lifecycle, you must obtain a reference to it. The App.get method retrieves an existing application by name from the Flyte backend.
import flyte.remote
# Retrieve an app by name. Project and domain default to your local configuration.
remote_app = flyte.remote.App.get(name="my-calculator-app")
print(f"App Name: {remote_app.name}")
print(f"Current Revision: {remote_app.revision}")
If the application is not found, the underlying AppService will raise a ConnectError with a NOT_FOUND code. Internally, App.get uses get_init_config() from flyte._initialize to resolve the organization, project, and domain if they are not explicitly provided.
Starting and Stopping Apps
Applications in flyte-sdk have a "desired state" (e.g., STARTED or STOPPED) and a "deployment status" (e.g., ACTIVE, FAILED). You control the desired state using activate() and deactivate().
Activating an App
To start an application and make its endpoint available, call activate(). By default, this is an asynchronous request to the backend.
# Request activation and return immediately
remote_app.activate()
# Request activation and wait until the app is fully ready
remote_app.activate(wait=True)
When wait=True is passed, the method calls watch(wait_for="activated") internally, which polls the backend until the deployment status reaches an active state.
Deactivating an App
To stop an application and release its resources, use deactivate().
# Stop the app and wait for it to reach the deactivated state
remote_app.deactivate(wait=True)
Monitoring Deployment Progress
Because deployment is an asynchronous process on the Flyte backend, you often need to check if an app has reached its target state or monitor it until it does.
Checking Status
You can inspect the current state of an app using boolean helpers or the raw deployment status:
if remote_app.is_active():
print(f"App is ready at {remote_app.endpoint}")
if remote_app.is_deactivated():
print("App is currently stopped.")
# Access the raw Protobuf enum status
status = remote_app.deployment_status
The is_active() method checks if the status is either DEPLOYMENT_STATUS_ACTIVE or DEPLOYMENT_STATUS_STARTED. Conversely, is_deactivated() checks for DEPLOYMENT_STATUS_UNASSIGNED or DEPLOYMENT_STATUS_STOPPED.
Watching for Transitions
The watch() method provides a way to block execution until an application reaches a specific state. It uses a gRPC stream via AppService.watch to receive real-time updates.
try:
# Block until the app is activated
updated_app = remote_app.watch(wait_for="activated")
except RuntimeError as e:
# Raised if the deployment fails or stalls
print(f"Deployment failed: {e}")
If the backend reports a DEPLOYMENT_STATUS_FAILED, watch() raises a RuntimeError.
Using Ephemeral Contexts
For temporary deployments—such as running a suite of tests against a live app or performing a one-off calculation—flyte-sdk provides context managers that automate the activation and deactivation cycle.
Synchronous Context
The ephemeral_ctx_sync() method ensures the app is activated before your code runs and deactivated immediately after, even if an error occurs.
with remote_app.ephemeral_ctx_sync():
# The app is guaranteed to be active here
print(f"Performing operations at {remote_app.endpoint}")
# ... your logic here ...
# The app is guaranteed to be deactivated here
assert remote_app.is_deactivated()
Asynchronous Context
If you are working in an async environment, use ephemeral_ctx():
async with remote_app.ephemeral_ctx():
await perform_async_tasks(remote_app.endpoint)
Both context managers call activate(wait=True) on entry and deactivate(wait=True) on exit, ensuring the application is in the correct state before and after the block.
Inspecting App Metadata
The App class provides several properties to inspect the application's configuration and access points:
endpoint: The public URL where the application is reachable.url: The Flyte Console URL for viewing the application's details in a browser.revision: The specific version number of the application deployment.
print(f"Access the app at: {remote_app.endpoint}")
print(f"View in Console: {remote_app.url}")
The url property is generated dynamically by calling client.console.app_url, ensuring it points to the correct Flyte console instance based on your environment configuration.
Internal Mechanism: AppService
The App class is a high-level wrapper around the AppService protocol defined in flyte.remote._client._protocols. Every lifecycle method maps to a gRPC call:
App.getcallsAppService.getApp.activate/deactivatecallsAppService.updateApp.watchcallsAppService.watchApp.deletecallsAppService.delete
This separation allows the App class to provide a clean, Pythonic API (including syncify support for both sync and async usage) while the AppService handles the low-level communication with the Flyte backend.