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: include serialization listeners when serving/running from CLI #66

Merged
merged 1 commit into from
Jan 26, 2024
Merged
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
18 changes: 11 additions & 7 deletions projects/fal/src/fal/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ def by_value_locator(obj, pickler=None, og_locator=_dill._locate_function):
_dill._locate_function = by_value_locator


def include_packages_from_path(raw_path: str):
path = Path(raw_path)
parent = path
while (parent.parent / "__init__.py").exists():
parent = parent.parent

if parent != path:
_PACKAGES.add(parent.name)


def add_serialization_listeners_for(obj):
module_name = getattr(obj, "__module__", None)
if not module_name:
Expand All @@ -61,13 +71,7 @@ def add_serialization_listeners_for(obj):
# tree to locate the actual package name.
import __main__

path = Path(__main__.__file__)
parent = path
while (parent.parent / "__init__.py").exists():
parent = parent.parent

if parent != path:
_PACKAGES.add(parent.name)
include_packages_from_path(__main__.__file__)

if "." in module_name:
package_name, *_ = module_name.partition(".")
Expand Down
6 changes: 5 additions & 1 deletion projects/fal/src/fal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import click
import fal.auth as auth
import fal
from fal import api, sdk
from fal import api, sdk, _serialization
from fal.console import console
from fal.exceptions import ApplicationExceptionHandler
from fal.logging import get_logger, set_debug_logging
Expand Down Expand Up @@ -246,6 +246,10 @@ def load_function_from(
if function_name not in module:
raise api.FalServerlessError(f"Function '{function_name}' not found in module")

# The module for the function is set to <run_path> when runpy is used, in which
# case we want to manually include the packages it is defined in.
_serialization.include_packages_from_path(file_path)

target = module[function_name]
if isinstance(target, type) and issubclass(target, fal.App):
target = fal.wrap_app(target, host=host)
Expand Down
Loading