Skip to content

run

faststream.cli.main.run #

run(
    ctx: typer.Context,
    app: str = typer.Argument(
        ...,
        help="[python_module:FastStream] - path to your application",
    ),
    workers: int = typer.Option(
        1,
        show_default=False,
        help="Run [workers] applications with process spawning",
    ),
    log_level: LogLevels = typer.Option(
        LogLevels.info,
        case_sensitive=False,
        show_default=False,
        help="[INFO] default",
    ),
    reload: bool = typer.Option(
        False,
        "--reload",
        is_flag=True,
        help="Restart app at directory files changes",
    ),
    app_dir: Optional[str] = typer.Option(
        None,
        "--app-dir",
        help="Look for APP in the specified directory, by adding this to the PYTHONPATH. Defaults to the current working directory.",
    ),
) -> None

Run [MODULE:APP] FastStream application

Source code in faststream/cli/main.py
@cli.command(
    context_settings={"allow_extra_args": True, "ignore_unknown_options": True}
)
def run(
    ctx: typer.Context,
    app: str = typer.Argument(
        ...,
        help="[python_module:FastStream] - path to your application",
    ),
    workers: int = typer.Option(
        1,
        show_default=False,
        help="Run [workers] applications with process spawning",
    ),
    log_level: LogLevels = typer.Option(
        LogLevels.info,
        case_sensitive=False,
        show_default=False,
        help="[INFO] default",
    ),
    reload: bool = typer.Option(
        False,
        "--reload",
        is_flag=True,
        help="Restart app at directory files changes",
    ),
    app_dir: Optional[str] = typer.Option(
        None,
        "--app-dir",
        help=(
            "Look for APP in the specified directory, by adding this to the PYTHONPATH."
            " Defaults to the current working directory."
        ),
    ),
) -> None:
    """Run [MODULE:APP] FastStream application"""
    app, extra = parse_cli_args(app, *ctx.args)
    casted_log_level = get_log_level(log_level)

    if app_dir:
        sys.path.insert(0, app_dir)

    args = (app, extra, casted_log_level)

    if reload and workers > 1:
        raise ValueError("You can't use reload option with multiprocessing")

    if reload is True:
        from faststream.cli.supervisors.watchfiles import WatchReloader

        module_path, _ = import_from_string(app)

        WatchReloader(
            target=_run,
            args=args,
            reload_dirs=[str(module_path)] + ([app_dir] if app_dir else []),
        ).run()

    elif workers > 1:
        from faststream.cli.supervisors.multiprocess import Multiprocess

        Multiprocess(
            target=_run,
            args=(*args, logging.DEBUG),
            workers=workers,
        ).run()

    else:
        _run(
            app=app,
            extra_options=extra,
            log_level=casted_log_level,
        )

Last update: 2023-11-13