انتقل إلى المحتوى

مرجع واجهة ⁦node:child_process⁩

العمليات الفرعية المتزامنة وغير المتزامنة، والعمال المتشعّبون مع ⁦IPC⁩

في هذه الصفحة
Open in ChatGPTOpen in Claude

تعمل مساعدات العمليات الفرعية غير المتزامنة والمتزامنة كلتاهما في السكربتات العادية. ويُبقي الابن الجاري السكربت حيًّا حتى حدث ⁦close⁩ الخاص به:

import { spawn, execFileSync } from "node:child_process";

const child = spawn("echo", ["hello"]);
child.stdout.on("data", (data) => console.log(String(data).trim())); // hello
child.on("close", (code) => console.log("exit", code)); // exit 0

const output = execFileSync(process.execPath, [
  "--snippet",
  'process.stdout.write("hi")',
]);
console.log(output.toString()); // hi

الصادرات المتاحة هي ⁦spawn⁩ و⁦spawnSync⁩ و⁦exec⁩ و⁦execSync⁩ و⁦execFile⁩ و⁦execFileSync⁩ و⁦fork⁩ و⁦ChildProcess⁩. يعيد ⁦spawnSync⁩ الكائن المألوف الذي يتضمن ⁦pid⁩ و⁦status⁩ و⁦signal⁩ و⁦stdout⁩ و⁦stderr⁩ و⁦output⁩. ويبدأ ⁦fork()⁩ سكربت ⁦Elide⁩ آخر مع قناة ⁦IPC⁩: يتبادل ⁦child.send()⁩ و ⁦process.send()⁩ الرسائل، التي تُسلَّم كأحداث ⁦message⁩.

يتحكم في إنشاء العمليات ⁦--allow-run[=CMDS]⁩ و ⁦--deny-run <CMDS>⁩ عندما تكون خيارات العزل نشطة.

Reference

Status: Full · Not measured · since 1.1.0

Exports

ExportKindStatusNotes
exec(), execSync()functionFullExecutes a command in a shell
execFile(), execFileSync()functionFullExecutes a file without a shell
spawn(), spawnSync()functionFullSpawns a new process using the given command
fork()functionFullSpawns a new Node.js process using the given module path

هل كانت هذه الصفحة مفيدة؟