Deno 運(yùn)行子進(jìn)程

2020-06-24 17:07 更新

示例:

  1. // 創(chuàng)建子進(jìn)程
  2. const p = Deno.run({
  3. cmd: ["echo", "hello"],
  4. });
  5. // 等待完成
  6. await p.status();

運(yùn)行

  1. $ deno run --allow-run ./subprocess_simple.ts
  2. hello

window.onload 被賦值為一個(gè)函數(shù),它將會(huì)在主腳本加載后被調(diào)用,和瀏覽器的 onload 一樣,可以用于主入口點(diǎn)。 默認(rèn)情況下,當(dāng)您調(diào)用 Deno.run() 時(shí),子進(jìn)程將繼承父進(jìn)程的標(biāo)準(zhǔn)流。如果您想要和子進(jìn)程通信,可以使用 "piped" 選項(xiàng)。

  1. const fileNames = Deno.args;
  2. const p = Deno.run({
  3. cmd: [
  4. "deno",
  5. "run",
  6. "--allow-read",
  7. "https://deno.land/std/examples/cat.ts",
  8. ...fileNames,
  9. ],
  10. stdout: "piped",
  11. stderr: "piped",
  12. });
  13. const { code } = await p.status();
  14. if (code === 0) {
  15. const rawOutput = await p.output();
  16. await Deno.stdout.write(rawOutput);
  17. } else {
  18. const rawError = await p.stderrOutput();
  19. const errorString = new TextDecoder().decode(rawError);
  20. console.log(errorString);
  21. }
  22. Deno.exit(code);

運(yùn)行

  1. $ deno run --allow-run ./subprocess.ts <somefile>
  2. [file content]
  3. $ deno run --allow-run ./subprocess.ts non_existent_file.md
  4. Uncaught NotFound: No such file or directory (os error 2)
  5. at DenoError (deno/js/errors.ts:22:5)
  6. at maybeError (deno/js/errors.ts:41:12)
  7. at handleAsyncMsgFromRust (deno/js/dispatch.ts:27:17)
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)