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

# 流式输出结果

export const SandboxConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>在运行本文档中的示例代码前，请确保您已正确配置环境变量，详情请参考 <a href="/sandbox/get-start#配置环境变量">配置环境变量</a>。</Note>;
  }
};

<SandboxConfigHint />

要在命令执行时流式输出结果，请在 JavaScript 中向 `commands.run()` 方法传递 `onStdout`、`onStderr` 回调，或在 Python 中向 `commands.run()` 方法传递 `on_stdout`、`on_stderr` 回调。

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={null}
  import { Sandbox } from 'ppio-sandbox/code-interpreter'

  const sandbox = await Sandbox.create()
  const result = await sandbox.commands.run('echo hello; sleep 1; echo world', {
    onStdout: (data) => {
      console.log(data)
    },
    onStderr: (data) => {
      console.log(data)
    },
  })

  console.log(result)

  await sandbox.kill()
  ```

  ```python Python icon="python" theme={null}
  from ppio_sandbox.code_interpreter import Sandbox

  sandbox = Sandbox.create()

  result = sandbox.commands.run('echo hello; sleep 1; echo world',
                                on_stdout=lambda data: print(
                                    data),
                                on_stderr=lambda data: print(data))

  print(result)

  sandbox.kill()
  ```
</CodeGroup>
