xiongxiao hat die Gist bearbeitet . Zu Änderung gehen
1 file changed, 38 insertions
process-fork-call.ts(Datei erstellt)
| @@ -0,0 +1,38 @@ | |||
| 1 | + | import { App } from '@kevisual/router' | |
| 2 | + | import { fork } from 'child_process' | |
| 3 | + | const app = new App() | |
| 4 | + | import path from 'path' | |
| 5 | + | ||
| 6 | + | // http://localhost:3000/api/router?path=call | |
| 7 | + | app.route({ | |
| 8 | + | path: 'call' | |
| 9 | + | }).define(async (ctx) => { | |
| 10 | + | ctx.body = 'Hello World' | |
| 11 | + | const pwd = process.cwd() | |
| 12 | + | const testA = path.join(pwd, 'src/test/a.ts') | |
| 13 | + | const child = fork(testA, { | |
| 14 | + | }) | |
| 15 | + | console.log('Child process started with PID:', child.pid) | |
| 16 | + | child.on('message', (msg) => { | |
| 17 | + | console.log('Message from child', msg) | |
| 18 | + | setTimeout(() => { | |
| 19 | + | console.log('child process connected:', child.connected) | |
| 20 | + | console.log('child process killed:', child.killed) | |
| 21 | + | console.log('child process exit code:', child.exitCode) | |
| 22 | + | }, 20) | |
| 23 | + | }) | |
| 24 | + | ||
| 25 | + | child.on('exit', (code, signal) => { | |
| 26 | + | console.log('子进程已退出,退出码:', code, '信号:', signal) | |
| 27 | + | }) | |
| 28 | + | ||
| 29 | + | child.on('close', (code, signal) => { | |
| 30 | + | console.log('子进程已关闭,退出码:', code, '信号:', signal) | |
| 31 | + | }) | |
| 32 | + | child.send({ hello: 'world' }) | |
| 33 | + | ||
| 34 | + | }).addTo(app) | |
| 35 | + | ||
| 36 | + | app.listen(3000, () => { | |
| 37 | + | console.log('Server is running on http://localhost:3000') | |
| 38 | + | }) | |
Neuer
Älter