node.js中的子进程神秘地退出同步代码行之间 [英] Child process in node.js mysteriously exiting between lines of synchronous code

查看:145
本文介绍了node.js中的子进程神秘地退出同步代码行之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在node.js的子进程中遇到了一个奇怪的问题,我在子进程中运行了一些类似的代码,但是子进程在同步代码行之间以代码0 退出。

  var fs = require('fs'); 
var http = require(’http’);
var mkdirp = require(’mkdirp’);


var urlPath = __dirname + \\urls.txt;
var savePath = __dirname + \\;
var objects = [];
var globalI = 0;

var文件;

if(!fs.existsSync(urlPath))
{
console.log( + urlPath +处的文件不存在!);
}
else
{
if(!fs.existsSync(savePath)){
mkdirp.sync(savePath);
}

console.log(找到文件!正在读取...);
console.log(仍在运行);

try {
var data = fs.readFileSync(urlPath,{ encoding: utf8});
} catch(err){
console.log(读取url文件时出错...);
抛出错误;
}最终{
console.log(文件已读!);
var array = data.split( \n);

console.log(找到 + array.length + URLs);
}

线段

  console.log(找到文件!正在读取...); 

运行并显示在控制台中。但是,下一行

  console.log(仍在运行); 

不运行。子进程在该行代码之前退出。我完全不知道为什么。



此外,如果我更改两个语句的顺序,它仍然只在退出前执行第一个语句。



编辑



所以也许它确实与刷新和其他错误有关。如果我删除了这两个连续的日志语句,它将在最终块中运行下一个日志语句,然后退出。



EDIT2



对于此问题,还有其他一些好奇之处。您可以在代码片段中看到我有一个名为 globalI
的变量。稍后在代码中,该变量就像 globalI ++一样递增;
如果我不对增量进行注释,则子进程会意外退出,但这没有任何意义,因为它甚至永远不会接近增量发生时的行



这实际上是我开始遇到此问题的方式。我完全不知所措

解决方案

最后,您仍然缺少},但是我非常严重地怀疑这与它有任何关系,并且



在我看来,您可能在刷新stdout之前可能已经退出了程序。



在人们遇到与您的问题非常相似的地方阅读此线程:

p>

https://github.com/joyent/ node / issues / 1669



基于您在此之后执行readFileSync调用的事实,这似乎令人难以置信,但我想仍然有可能。 / p>

您可以做的一件事就是测试一下,在两次写入之前都设置一个超时,例如5秒。这将使程序保持5秒钟的运行状态,这是清除控制台日志的足够时间。

  ... 
setTimeout(function(){console.log( Timeout !!);},5000);
console.log(找到文件!正在读取...);
console.log(仍在运行);
...

如果两行都说清楚了,那么您就知道自己有时间了问题。如果在那之后仍然无法解决问题,那么至少可以消除计时问题。



也许您会遇到某种无法处理的错误。您可以放入以下代码,看看是否被触发:

  process.on('uncaughtException',function(err) {
console.error('uncaught:'+ err);
});


I'm having a weird problem with child process in node.js I have some code running in a child process like this, but the child process exits with code 0 in between lines of synchronous code.

var fs = require('fs');
var http = require('http');
var mkdirp = require('mkdirp');


var urlPath = __dirname + "\\urls.txt";
var savePath = __dirname + "\\";
var objects = [];
var globalI = 0;

var file;

if (!fs.existsSync(urlPath))
{
    console.log("File at " + urlPath + " does not exist!");
}
else 
{
    if(!fs.existsSync(savePath)){
        mkdirp.sync(savePath);
    }

    console.log("File found! Reading...");
    console.log("still running");

    try{
        var data = fs.readFileSync(urlPath, {"encoding":"utf8"});
    } catch (err) {
        console.log("Error reading url file...");
        throw err;
    } finally {
        console.log("File read!");
        var array = data.split("\n");

        console.log("Found " + array.length + " urls");
    }

The line

console.log("File found! Reading...");

Runs and appears in the console. However the next line

console.log("still running");

does not run. The child process exits before that line of code. I have absolutely no idea why. Any insight would be tremendously appreciated!

Also, if I change the order of the two statements, it still only executes the first before exiting.

EDIT

So maybe it does have to do with flushing and that other bug. If I remove both of those consecutive log statements, it runs the next log statement in the finally block and then quits.

EDIT2

There is also something else curious about this problem. You can see in the code snippet I have a variable called globalI Later on in the code, that variable gets incremented simply like globalI++; If I uncomment the incrementation, the child process stops exiting unexpectedly But it makes no sense, because it never even comes close to the line where the incrementation happens when it does exit unexpectedly.

That is actually how I started having this problem. I am completely flabbergasted

解决方案

You are missing a } after finally, but I very seriously doubt that has anything to do with it and was probably just a copy past error.

It looks to me like you might possibly be exiting the program before stdout can be flushed. There used to be a flush call you could explicitly make in older node versions, but not anymore.

Read this thread where people are having very similar issues to yours:

https://github.com/joyent/node/issues/1669

This seems hard to believe based on the fact you are doing a readFileSync call after that, but I suppose it's still possible.

One thing you could do to test it is to simply set a timeout before both your writes for something ridiculous like 5 seconds. This will keep the program alive for 5 seconds, more than enough time for any console logs to get flushed out.

...
setTimeout(function() { console.log("Timeout!!");  }, 5000);
console.log("File found! Reading...");
console.log("still running");
...

If both lines make it out, then you know you have a timing issue. If it's still busted after that at least you can eliminate timing as an issue.

Maybe you are somehow getting an unhandled error. You can put the following code in and see if it gets triggered:

process.on('uncaughtException', function (err) {
  console.error('uncaught: ' + err);
});

这篇关于node.js中的子进程神秘地退出同步代码行之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆