Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Runner async_run and async_resume #2033

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions metaflow/runner/metaflow_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

from metaflow import Run, metadata

from .utils import clear_and_set_os_environ, read_from_file_when_ready
from .utils import (
clear_and_set_os_environ,
read_from_file_when_ready,
async_read_from_file_when_ready,
)
from .subprocess_manager import CommandManager, SubprocessManager


Expand Down Expand Up @@ -294,6 +298,28 @@ def __get_executing_run(self, tfp_runner_attribute, command_obj):
error_message += "\nStderr:\n%s\n" % stderr_log
raise RuntimeError(error_message) from e

async def __async_get_executing_run(self, tfp_runner_attribute, command_obj):
try:
clear_and_set_os_environ(self.old_env)

content = await async_read_from_file_when_ready(
tfp_runner_attribute.name, command_obj, timeout=self.file_read_timeout
)
metadata_for_flow, pathspec = content.rsplit(":", maxsplit=1)
metadata(metadata_for_flow)
run_object = Run(pathspec, _namespace_check=False)
return ExecutingRun(self, command_obj, run_object)
except (CalledProcessError, TimeoutError) as e:
stdout_log = open(command_obj.log_files["stdout"]).read()
stderr_log = open(command_obj.log_files["stderr"]).read()
command = " ".join(command_obj.command)
error_message = "Error executing: '%s':\n" % command
if stdout_log.strip():
error_message += "\nStdout:\n%s\n" % stdout_log
if stderr_log.strip():
error_message += "\nStderr:\n%s\n" % stderr_log
raise RuntimeError(error_message) from e
Comment on lines +312 to +321
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not great to have the exception handling copied over from __get_executing_run?


def run(self, **kwargs) -> ExecutingRun:
"""
Blocking execution of the run. This method will wait until
Expand Down Expand Up @@ -395,7 +421,9 @@ async def async_run(self, **kwargs) -> ExecutingRun:
)
command_obj = self.spm.get(pid)

return self.__get_executing_run(tfp_runner_attribute, command_obj)
return await self.__async_get_executing_run(
tfp_runner_attribute, command_obj
)

async def async_resume(self, **kwargs):
"""
Expand Down Expand Up @@ -430,7 +458,9 @@ async def async_resume(self, **kwargs):
)
command_obj = self.spm.get(pid)

return self.__get_executing_run(tfp_runner_attribute, command_obj)
return await self.__async_get_executing_run(
tfp_runner_attribute, command_obj
)

def __exit__(self, exc_type, exc_value, traceback):
self.spm.cleanup()
Expand Down
16 changes: 16 additions & 0 deletions metaflow/runner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ def read_from_file_when_ready(
time.sleep(0.1)
content = file_pointer.read()
return content


async def async_read_from_file_when_ready(
file_path: str, command_obj: "CommandManager", timeout: float = 5
):
import asyncio

await asyncio.wait_for(command_obj.process.wait(), timeout)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raises TimeoutError on timeout, but with a different description than the sync version.


with open(file_path, "r", encoding="utf-8") as file_pointer:
content = file_pointer.read()
if not content:
raise CalledProcessError(
command_obj.process.returncode, command_obj.command
)
return content
Loading