我在为我的子进程提供相同的模块或对象时遇到问题。由于无法将普通对象发送到我的子进程(我只能发送“服务器”、套接字或 JSON 对象),因此我尝试在主 app.js 中创建一个模块,在它上面设置任何必要的数据,比在我的子进程中,我也需要它。
问题是,在我的 Child 进程中,我得到了一个不同的对象,而不是我在 app.js 中创建的那个。我尝试使用一个普通对象,并在其上设置一个属性,以及一个函数......
//module file
var Data = function(){
this.data = null;
return this;
};
module.exports = Data;
Run Code Online (Sandbox Code Playgroud)
……我是这样用的……
//app.js
var module = require("moduleName")();
module.data = "something";
Run Code Online (Sandbox Code Playgroud)
...但是在我的 Child 进程中,数据仍然为空
//Child process
var module = require("moduleName")();
console.log(module.data); --> null
Run Code Online (Sandbox Code Playgroud)
两个文件(app.js 和子进程的文件)都在同一个项目和目录中。
我唯一的猜测是,因为我创建了我的 Child 进程fork(),它可能会为模块使用不同的缓存......
那么有什么方法可以实现,在我的子进程中获取相同的对象,或者有没有办法将对象(不是 JSON)发送到我的子进程?
提前致谢!
如何打印标准输出 console.log(require("child_process").execSync('ls'))
我试过 ts
import { execSync } from 'child_process';
console.log(execSync('ls -la'));
Run Code Online (Sandbox Code Playgroud)
然后编译成js:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
console.log(child_process_1.execSync('ls -la'));
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我只得到缓冲区如何获得标准输出?
$ node app.js
$ <Buffer 74 6f 74 61 6c 20 38 38 0a 64 72 77 78 72 2d 78 72 2d 78 20 20 31 31 20 74 6f 6d 65 72 2e 62 65 6e 64 61 76 69 64 20 20 73 74 61 66 66 20 20 …Run Code Online (Sandbox Code Playgroud) 我正在cmd使用以下命令杀死一些任务:
taskkill /f /im software* /t
Run Code Online (Sandbox Code Playgroud)
它完成工作并用它杀死所有任务IMAGENAME;但是,看看它给了我什么,我看到了一些有趣的东西。往下看:
Run Code Online (Sandbox Code Playgroud)SUCCESS: The process with PID 14712 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 12184 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 16344 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 6816 (child process of PID 9068) has been terminated. SUCCESS: The process with PID 10656 (child process of PID 9068) has been …
我正在构建一个在 Windows 上运行的电子应用程序来移动机器人。(根据客户的要求,控制设备必须是Windows)它是用打字稿内置在Ionic中的,我正在尝试创建一个桌面解决方案,其功能是运行一些.exe文件,以按照一些预定义的动作移动机器人。它们是由另一个人制作的一组 .exe 文件,从 shell 运行时可以正常工作。这些 exe 文件确实可以关闭/打开灯、移动手臂等...现在这些 exe 文件可以工作,并且它们都位于 C:\botcontrol\ 文件夹中。
我一直在努力让节点child_process能够从一个简单的界面运行所有这些 exe 文件。我的问题是我无法child_process以任何方式包含而不会出现控制台错误t(...).execFile is not a function。
我已经在互联网上用各种语言挖掘了两天关于如何将child_process节点模块包含在打字稿角度7应用程序中,但我真的坚持这个,我不知道如何解决它而不得到该诅咒的错误多于。我尝试添加到 tsconfig:
"map": {
"child_process": "@node/child_process"
}
Run Code Online (Sandbox Code Playgroud)
和许多其他 webpack 配置尝试,但没有一个对我有用。
现在我需要的是添加“child_process”,这样当我运行服务时我可以像这样导入它:
import { exec,execFile} from "child_process";
...
Run Code Online (Sandbox Code Playgroud)
或者
import * as cp from "child_process";
...
Run Code Online (Sandbox Code Playgroud)
我读到一些内容说我需要添加要在 SystemJS 或 webpack 配置中导出的模块,但我在我的项目中没有找到任何类似的配置。
我的依赖项位于 package.json 中:
...
"dependencies": {
"@angular/common": "~8.1.2",
"@angular/compiler": "~8.1.2",
"@angular/core": "~8.1.2",
"@angular/forms": "~8.1.2",
"@angular/platform-browser": "~8.1.2",
"@angular/platform-browser-dynamic": "~8.1.2",
"@angular/router": "~8.1.2",
"@ionic-native/core": "^5.14.0", …Run Code Online (Sandbox Code Playgroud) face_detection我有一个进行命令行调用的异步函数。否则一切正常,但我无法等待回复。这是我的功能:
async uploadedFile(@UploadedFile() file) {
let isThereFace: boolean;
const foo: child.ChildProcess = child.exec(
`face_detection ${file.path}`,
(error: child.ExecException, stdout: string, stderr: string) => {
console.log(stdout.length);
if (stdout.length > 0) {
isThereFace = true;
} else {
isThereFace = false;
}
console.log(isThereFace);
return isThereFace;
},
);
console.log(file);
const response = {
filepath: file.path,
filename: file.filename,
isFaces: isThereFace,
};
console.log(response);
return response;
}
Run Code Online (Sandbox Code Playgroud)
isThereFace在我的响应中,我返回始终是undefined因为响应是在响应face_detection准备好之前发送给客户端的。我怎样才能做到这一点?
const {execSync , exec} = require('child_process')
const res = execSync("help")
console.log(res.toString())
Run Code Online (Sandbox Code Playgroud)
对于我尝试执行的每个命令(在本例中为“帮助”),它会抛出错误“错误:命令失败...未找到”。我不明白的是?
我正在尝试在另一个进程中创建子进程.我用C语言编写这两个程序.首先,我编写一个虚拟进程,它将是子进程.它所做的只是在屏幕上写一个字符串.它本身很好用.然后我写另一个程序,它将是父进程.但是,我无法实现.我正在尝试将fork和execl函数一起使用,但是我失败了.我还希望子进程在父进程终止之前不会终止.
我该如何编写父进程?
谢谢.
以下是子进程的代码:
#include <stdio.h>
int main(void) {
while(1) {
printf("*");
sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
这是父进程:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
if (fork()) {
while(1) {
printf("-\n");
sleep(5);
}
} else {
execl("./", "dummy", (char *)0);
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试执行*.exe文件,但得到:
exec error: { Error: spawn ${__dirname}/install.exe ENOENT
代码:
var execFile = require('child_process').execFile
execFile('${__dirname}/install.exe', function(error, stderr) {
console.log('stderr: ', __dirname);
if (error !== null) {
console.log('exec error: ', error);
}
});
Run Code Online (Sandbox Code Playgroud)
还尝试过:'${__dirname}\install.exe', './install.exe','D:\install.exe'
我正在尝试从以python编写的秤流式传输输出。该程序(scale.py)连续运行并每半秒打印一次原始值。
import RPi.GPIO as GPIO
import time
import sys
from hx711 import HX711
def cleanAndExit():
print "Cleaning..."
GPIO.cleanup()
print "Bye!"
sys.exit()
hx = HX711(5, 6)
hx.set_reading_format("LSB", "MSB")
hx.reset()
hx.tare()
while True:
try:
val = hx.get_weight(5)
print val
hx.power_down()
hx.power_up()
time.sleep(0.5)
except (KeyboardInterrupt, SystemExit):
cleanAndExit()
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过打印一个单独的NodeJs程序(位于同一文件夹中的index.js)获取每个原始数据点print val。这是我的节点程序。
var spawn = require('child_process').spawn;
var py = spawn('python', ['scale.py']);
py.stdout.on('data', function(data){
console.log("Data: " + data);
});
py.stderr.on('data', function(data){
console.log("Error: " + data);
});
Run Code Online (Sandbox Code Playgroud)
当我运行时sudo node index.js,没有输出,程序将永久保存。
我的想法是print val将输出放入stdout流中,并 …
以下是我的代码的主要部分(采用"高级linux编程",清单3.7):
void clean_up_child_process ( int signal_number ) {
/* Clean up the child process. */
int status ;
wait ( &status ) ;
printf ( " wait finished \n" ) ;
/* Store its exit status in a global variable. */
child_exit_status = status ;
}
int main() {
/* Handle SIGCHLD by calling clean_up_child_process. */
pid_t child_pid ;
struct sigaction sigchld_action ;
memset ( &sigchld_action, 0, sizeof(sigchld_action) ) ;
sigchld_action.sa_handler = &clean_up_child_process ;
sigaction ( SIGCHLD, &sigchld_action, NULL ) …Run Code Online (Sandbox Code Playgroud) child-process ×10
node.js ×6
javascript ×3
c ×2
async-await ×1
cmd ×1
electron ×1
fork ×1
parent-child ×1
powershell ×1
process ×1
python ×1
require ×1
spawn ×1
stdout ×1
typescript ×1
unix ×1
webpack ×1
windows ×1