> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 원격 함수

> Run Python functions inside sandboxes with automatic 직렬화.

이 예시에서는 `CWSANDBOX_API_KEY`에 설정된 CoreWeave API 액세스 토큰을 사용합니다. Forge API 키 인증은 [시작하기](/ko/products/sandboxes/get-started)를 참조하세요. CKS 요구 사항은 [배치 모드](/ko/products/sandboxes/placement)를 참조하세요.

이 튜토리얼에서는 원격 함수를 사용하여 샌드박스 내에서 Python 함수를 실행하는 방법을 안내합니다. 셸 명령어 문자열을 구성하는 대신 샌드박스에서 실행되는 코드를 로컬 함수처럼 호출할 수 있습니다. Python 코드에서 직접 샌드박스의 컴퓨팅 작업을 호출하고 유형 지정된 결과를 수집하려는 개발자를 위한 튜토리얼입니다.

<h2 id="define-and-call-a-remote-function">
  원격 함수 정의 및 호출
</h2>

함수에 `@session.function()` 데코레이터를 적용하면 `.remote()`로 샌드박스 내부에서 호출할 수 있습니다. 호출은 `.result()`로 확인할 수 있는 참조를 반환합니다:

```python theme={"system"}
from cwsandbox import Sandbox, SandboxDefaults

with Sandbox.session(SandboxDefaults()) as session:
    @session.function()
    def add(x: int, y: int) -> int:
        return x + y

    result = add.remote(2, 3).result()  # 5
```

<h2 id="run-functions-in-parallel-with-map">
  `.map()`으로 함수를 병렬 실행하기
</h2>

`.map()`을 사용하면 여러 호출을 한 번에 디스패치하고 그 결과를 수집할 수 있습니다. 서로 독립적인 호출은 하나씩 순차적으로 실행되지 않고 동시에 실행됩니다.

```python theme={"system"}
@session.function()
def square(x: int) -> int:
    return x * x

refs = square.map([(1,), (2,), (3,)])
results = [r.result() for r in refs]  # [1, 4, 9]
```

<h2 id="next-steps">
  다음 단계
</h2>

이제 샌드박스 내의 Python 함수를 로컬 호출처럼 호출하고, `.map()`으로 작업을 병렬로 분산할 수 있습니다.

직렬화 모드, `.local()` 테스트 등에 대한 자세한 내용은 [원격 함수 가이드](/ko/products/sandboxes/client/guides/remote-functions)를 참조하세요.
