Skip to content

Commit

Permalink
Add --factory option (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Sep 3, 2024
1 parent 55f7d22 commit 919d3b2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion granian/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def load_module(module_name: str, raise_on_failure: bool = True) -> Optional[Mod
return sys.modules[module_name]


def load_target(target: str) -> Callable[..., None]:
def load_target(target: str, factory: bool = False) -> Callable[..., None]:
sys.path.insert(0, '')
path, name = get_import_components(target)
path = prepare_import(path) if path else None
Expand All @@ -59,4 +59,6 @@ def load_target(target: str) -> Callable[..., None]:
rv = module
for element in name.split('.'):
rv = getattr(rv, element)
if factory:
rv = rv()
return rv
7 changes: 7 additions & 0 deletions granian/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def option(*param_decls: str, cls: Optional[Type[click.Option]] = None, **attrs:
type=click.IntRange(60),
help='The maximum amount of time in seconds a worker will be kept alive before respawn',
)
@option(
'--factory/--no-factory',
default=False,
help='Treat target as a factory function, that should be invoked to build the actual target',
)
@option(
'--reload/--no-reload',
default=False,
Expand Down Expand Up @@ -278,6 +283,7 @@ def cli(
respawn_failed_workers: bool,
respawn_interval: float,
workers_lifetime: Optional[int],
factory: bool,
reload: bool,
reload_paths: Optional[List[pathlib.Path]],
reload_ignore_dirs: Optional[List[str]],
Expand Down Expand Up @@ -336,6 +342,7 @@ def cli(
respawn_failed_workers=respawn_failed_workers,
respawn_interval=respawn_interval,
workers_lifetime=workers_lifetime,
factory=factory,
reload=reload,
reload_paths=reload_paths,
reload_ignore_paths=reload_ignore_paths,
Expand Down
4 changes: 3 additions & 1 deletion granian/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __init__(
respawn_failed_workers: bool = False,
respawn_interval: float = 3.5,
workers_lifetime: Optional[int] = None,
factory: bool = False,
reload: bool = False,
reload_paths: Optional[Sequence[Path]] = None,
reload_ignore_dirs: Optional[Sequence[str]] = None,
Expand Down Expand Up @@ -138,6 +139,7 @@ def __init__(
self.reload_on_changes = reload
self.respawn_interval = respawn_interval
self.workers_lifetime = workers_lifetime
self.factory = factory
self.reload_paths = reload_paths or [Path.cwd()]
self.reload_ignore_paths = reload_ignore_paths or ()
self.reload_ignore_dirs = reload_ignore_dirs or ()
Expand Down Expand Up @@ -680,7 +682,7 @@ def serve(
if wrap_loader:
target_loader = partial(target_loader, self.target)
else:
target_loader = partial(load_target, self.target)
target_loader = partial(load_target, self.target, factory=self.factory)

if not spawn_target:
spawn_target = default_spawners[self.interface]
Expand Down

0 comments on commit 919d3b2

Please sign in to comment.