App
App은 Modal에서 실행되는 프로그램의 단위입니다. 하나 이상의 Function을 묶어 관리하게 되며, 하나의 namespace를 구성하여 서로 다른 App 사이에는 이름이 충돌되지 않습니다.
import modal
app = modal.App()
def __init__(
self,
name: Optional[str] = None,
*,
tags: Optional[dict[str, str]] = None, # Additional metadata to set on the App
image: Optional[_Image] = None, # Default Image for the App (otherwise default to `modal.Image.debian_slim()`)
secrets: Sequence[_Secret] = [], # Secrets to add for all Functions in the App
volumes: dict[Union[str, PurePosixPath], _Volume] = {}, # Volume mounts to use for all Functions
include_source: bool = True, # Default configuration for adding Function source file(s) to the Modal container
) -> None:
Ephemeral Apps
modal run 명령어 또는 app.run( ) 메소드로 실행되는 App입니다. 임시로 만들어지는 App이며, 스크립트가 실행되는 동안만 지속됩니다. 프로세스가 종료되거나 클라이언트가 더 이상 서버와 연결되어 있지 않다고 판단되면 App도 종료됩니다.
def main():
# ...
with app.run():
some_modal_function.remote()
이런 방식으로 App을 실행하면 로그나 진행바 등이 나타나지는 않습니다. 만약 이를 원한다면, modal.enable_output으로 명시해주어야 합니다.
def main():
# ...
with modal.enable_output():
with app.run():
some_modal_function.remote()
Deployed Apps
modal deploy 명령어로 실행되는 App입니다. modal app stop 명령어 혹은 대시보드를 통해 직접 정지하지 않는 한, 반영구적으로 클라우드에 존재하게 됩니다.
Function
Function은 독립적인 실행 단위입니다. 하나의 App 안에 여러 개의 Function이 존재할 수 있지만, 이들은 모두 독립적으로 실행하거나 확장할 수 있습니다. 덕분에 서버에서 특정 Function을 호출하지 않으면 그 Function은 컨테이너에도 올라가지 않아 자원을 효율적으로 사용할 수 있습니다.
@app.function( ) 데코레이션을 통해 선언할 수 있습니다.
@app.function()
def f():
print("Hello world!")
@app.function()
def g():
print("Goodbye world!")
이런 식으로 정의된 함수는 App에 속하게 되고, 나중에 remote로 실행할수 있게 됩니다.
Entrypoint
Entrypoint는 ephemeral App에서 modal run명령어를 실행했을 때 가장 먼저 실행되는 코드를 의미합니다.
@app.local_entrypoint( ) 데코레이션을 통해 선언할 수 있습니다. 만약 App에 하나의 Function 만이 존재한다면, 데코레이션이 없어도 그 함수가 자동으로 entrypoint가 됩니다.
@app.local_entrypoint()
def main():
f.remote()
Argument parsing
Entrypoint 함수에 기본타입(int, str 등)의 매개변수를 사용하면, modal cli에서 자동으로 매개변수를 사용할 수 있도록 파싱해줍니다.
# script.py
@app.local_entrypoint()
def main(foo: int, bar: str):
some_modal_function.remote(foo, bar)
가령 위와 같이 int 타입의 foo와 str 타입의 bar를 매개변수를 사용했으면, 프롬프트에서 다음과 같이 사용할 수 있게 됩니다.
modal run script.py --foo 1 --bar "hello"
Entrypoint 직접 설정
기본적으로 modal run script.py는 local_entrypoint를 실행하지만, modal run script.py::app.f와 같이 직접 실행하고자 하는 함수를 지정할 수도 있습니다.
# script.py
@app.function()
def f():
print("Hello world!")
@app.function()
def g():
print("Goodbye world!")
@app.local_entrypoint()
def main():
f.remote()
정리 - modal에서 함수 실행 방법 3가지
1. local_entrypoint
@app.local_entrypoint()
def main():
func.remote()
modal run script.py로 실행할 수 있습니다.
2. __name__ = "__main__"
if __name__ == "__main__":
with app.run():
func.remote()
마찬가지로 modal run script.py로 실행할 수 있습니다.
3. web endpoint (fastapi_endpoint)
@app.function()
@modal.web_endpoint()
def main():
func1.remote()
# 또는
@app.function()
@modal.fastapi_endpoint()
def main():
func2.remote()
로컬에서 ephemeral App을 실행하는 것이 아닌, deployed App을 실행하는 방법입니다.
※ modal.web_endpoint( )가 modal.fastapi_endpoint( )로 대체되었다고 합니다.
'ML' 카테고리의 다른 글
| [Modal] 02. Function (1) | 2025.12.31 |
|---|---|
| [Modal] 01. App (1) | 2025.12.30 |
| [Modal] Modal에 대하여 (0) | 2025.12.28 |
| [GAN] Style 적대적 생성 신경망 (StyleGAN) (0) | 2023.04.28 |
| [GAN] 조건적 적대 생성 신경망 (CGAN) (0) | 2023.02.17 |