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

# 1 つのスクリプトから複数の run を起動するにはどうすればよいですか？

1 つのスクリプト内で複数の run をログするには、新しい run を開始する
前に、それまでの run をそれぞれ終了する必要があります。以下のセクションでは、
1 つのスクリプト内で run を順番に実行する 2 つのパターンと、複数の run を
同時にアクティブな状態に保つための別のパターンについて説明します。

<div id="use-a-context-manager">
  ## コンテキストマネージャーを使用する
</div>

`wandb.init()` をコンテキストマネージャーとして使用します。こうすると、スクリプトで例外が送出された場合に run が終了し、失敗としてマークされます：

```python theme={null}
import wandb

for x in range(10):
    with wandb.init() as run:
        for y in range(100):
            run.log({"metric": x + y})
```

<div id="call-runfinish-explicitly">
  ## run.finish を明示的に呼び出す
</div>

`run.finish()` を明示的に呼び出すこともできます：

```python theme={null}
import wandb

for x in range(10):
    run = wandb.init()

    try:
        for y in range(100):
            run.log({"metric": x + y})

    except Exception:
        run.finish(exit_code=1)
        raise

    finally:
        run.finish()
```

<div id="multiple-active-runs">
  ## 同時にアクティブな複数の run
</div>

`wandb` 0.19.10 以降では、run を順番に実行するのではなく同時に複数アクティブにする必要がある場合は、`reinit` 設定を `"create_new"` にして、複数の run を同時にアクティブに作成できます。

```python theme={null}
import wandb

with wandb.init(reinit="create_new") as tracking_run:
    for x in range(10):
        with wandb.init(reinit="create_new") as run:
            for y in range(100):
                run.log({"x_plus_y": x + y})

            tracking_run.log({"x": x})
```

`reinit="create_new"` の詳細 (W\&B
インテグレーションに関する注意事項を含む) については、[1 つのプロセスで複数の run](/ja/models/runs/initialize-run) を参照してください。

***

<Badge stroke shape="pill" color="orange" size="md">[Experiments](/ja/support/models/tags/experiments)</Badge>
