Última actividad 1760549903

process-fork-call.ts Sin formato
1import { App } from '@kevisual/router'
2import { fork } from 'child_process'
3const app = new App()
4import path from 'path'
5
6// http://localhost:3000/api/router?path=call
7app.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
36app.listen(3000, () => {
37 console.log('Server is running on http://localhost:3000')
38})