WASMプロセス
WASMプロセス
Section titled “WASMプロセス”WASMモジュールはprocess.wasmエントリ種別を通じてプロセスとして実行できます。プロセスはWippyプロセスホスト内で実行され、起動、監視、監督下シャットダウンの完全なプロセスライフサイクルをサポートします。
エントリ設定
Section titled “エントリ設定”entries: - name: wasm_binaries kind: fs.directory directory: ./wasm
- name: compute_worker kind: process.wasm fs: myns:wasm_binaries path: /worker.wasm hash: sha256:292b796376f8b4cc360acf2ea6b82d1084871c3607a079f30b446da8e5c984a4 method: compute設定フィールド
Section titled “設定フィールド”| フィールド | 必須 | 説明 |
|---|---|---|
fs | Yes | バイナリを含むファイルシステムエントリID |
path | Yes | ファイルシステム内の.wasmファイルへのパス |
hash | Yes | 整合性検証用のSHA-256ハッシュ |
method | Yes | 実行するエクスポート関数名 |
transport | No | 呼び出しトランスポート: payload(デフォルト)またはwasi-http |
wit | No | raw/coreモジュール用のWIT署名 |
imports | No | 有効にするホストインポート |
wasi | No | WASI設定(args、env、mounts) |
limits | No | 実行制限 |
CLIコマンド
Section titled “CLIコマンド”meta.commandでWASMプロセスを名前付きコマンドとして登録します:
- name: greet kind: process.wasm meta: command: name: greet short: Greet someone via WASM fs: myns:wasm_binaries path: /component.wasm hash: sha256:... method: greet以下で実行します:
wippy run greet利用可能なコマンドを一覧表示します:
wippy run list| フィールド | 必須 | 説明 |
|---|---|---|
name | Yes | wippy run <name>で使用するコマンド名 |
short | No | wippy run listに表示される短い説明 |
CLIコマンドが動作するにはterminal.hostとprocess.hostが必要です。
プロセスライフサイクル
Section titled “プロセスライフサイクル”WASMプロセスはInit/Step/Closeライフサイクルモデルに従います:
- Init - モジュールがインスタンス化され、入力引数がキャプチャされます
- Step - 実行が進みます。非同期モジュールの場合、スケジューラがyield/resumeサイクルを駆動します。同期モジュールの場合、実行は単一ステップで完了します。
- Close - インスタンスリソースが解放されます
Luaからのスポーン
Section titled “Luaからのスポーン”WASMプロセスをスポーンし、完了を監視します:
local process = require("process")local time = require("time")
-- Spawn with monitoringlocal pid, err = process.spawn_monitored( "myns:compute_worker", -- entry ID "myns:processes", -- process group 6, 7 -- arguments passed to the WASM function)
if err then error("spawn failed: " .. tostring(err))end
-- Wait for the process to completelocal events = process.events()local event = events:receive()if event and event.kind == process.event.EXIT then local result = event.result.value -- return value from the WASM functionendWASIインターフェースをインポートするWASMプロセスは非同期操作を実行できます。スケジューラはI/O中にプロセスをサスペンドし、操作完了時に再開します:
- name: http_worker kind: process.wasm fs: myns:wasm_binaries path: /http_worker.wasm hash: sha256:... method: run imports: - wasi:io - wasi:cli - wasi:http wasi: env: - id: myns:api_url name: API_URL required: trueyield/resumeメカニズムはWASMコードに対して透過的です。ゲスト内の標準的なブロッキング呼び出し(スリープ、読み取り、書き込み、HTTPリクエスト)は自動的にディスパッチャにyieldします。
WASI設定
Section titled “WASI設定”プロセスは関数と同じWASI設定をサポートします:
- name: file_processor kind: process.wasm fs: myns:wasm_binaries path: /processor.wasm hash: sha256:... method: process imports: - wasi:cli - wasi:io - wasi:clocks - wasi:filesystem wasi: args: ["--input", "/data/input.csv"] cwd: "/app" env: - id: myns:output_format name: OUTPUT_FORMAT mounts: - fs: myns:input_data guest: /data read_only: true - fs: myns:output_dir guest: /output