如何强行阻止Node.js进程终止? [英] How to forcibly keep a Node.js process from terminating?

查看:459
本文介绍了如何强行阻止Node.js进程终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

强制保持Node.js进程运行的最佳方法是什么,即保持其事件循环不为空,从而防止进程终止?我能想到的最好的解决方案是:

What is the best way to forcibly keep a Node.js process running, i.e., keep its event loop from running empty and hence keeping the process from terminating? The best solution I could come up with was this:

const SOME_HUGE_INTERVAL = 1 << 30;
setInterval(() => {}, SOME_HUGE_INTERVAL);

如果您将间隔时间保持足够长的时间,它将使间隔运行而不会造成太大干扰.

Which will keep an interval running without causing too much disturbance if you keep the interval period long enough.

有更好的方法吗?

我有一个使用 Edge.js 的Node.js脚本来注册回调函数,以便它可以从.NET中的DLL内部调用.每秒将调用此功能1次,发送一个简单的序列号,该序列号应打印到控制台.

I have a Node.js script using Edge.js to register a callback function so that it can be called from inside a DLL in .NET. This function will be called 1 time per second, sending a simple sequence number that should be printed to the console.

Edge.js部分很好,一切正常.我唯一的问题是我的Node.js进程执行其脚本,然后用完所有事件以进行处理.事件循环为空,它只是终止,而忽略了它应该一直运行以便能够从DLL接收回调的事实.

The Edge.js part is fine, everything is working. My only problem is that my Node.js process executes its script and after that it runs out of events to process. With its event loop empty, it just terminates, ignoring the fact that it should've kept running to be able to receive callbacks from the DLL.

我的Node.js脚本:

My Node.js script:

var
    edge = require('edge');

var foo = edge.func({
    assemblyFile: 'cs.dll',
    typeName: 'cs.MyClass',
    methodName: 'Foo'
});

// The callback function that will be called from C# code:
function callback(sequence) {
    console.info('Sequence:', sequence);
}

// Register for a callback:
foo({ callback: callback }, true);

// My hack to keep the process alive:
setInterval(function() {}, 60000);

我的C#代码(DLL):

My C# code (the DLL):

public class MyClass
{
    Func<object, Task<object>> Callback;

    void Bar()
    {
        int sequence = 1;

        while (true)
        {
            Callback(sequence++);
            Thread.Sleep(1000);
        }
    }

    public async Task<object> Foo(dynamic input)
    {
        // Receives the callback function that will be used:
        Callback = (Func<object, Task<object>>)input.callback;

        // Starts a new thread that will call back periodically:
        (new Thread(Bar)).Start();

        return new object { };
    }
}

我唯一能想到的解决方案是注册一个间隔很长的计时器来调用一个空函数,以使调度程序保持繁忙并避免使事件循环为空,从而使进程永远运行.

The only solution I could come up with was to register a timer with a long interval to call an empty function just to keep the scheduler busy and avoid getting the event loop empty so that the process keeps running forever.

有什么办法比我做得更好吗?也就是说,要保持该进程运行而不必使用这种"hack"?

Is there any way to do this better than I did? I.e., keep the process running without having to use this kind of "hack"?

推荐答案

尽管对于此问题似乎没有正确的答案,但到目前为止,我们有3种可能的破解方式,老实说,我认为我的方法是侵入性较小的方法:

Although it looks like there's no right answer to this question, so far we have 3 possible hacks and I honestly think my approach is the less intrusive one:

setInterval(() => {}, 1 << 30);

这将设置一个间隔,大约每12天触发一次.

This will set an interval that will fire approximately once every 12 days.

最初,我的黑客通过了Number.POSITIVE_INFINITY作为周期,因此计时器实际上不会触发,但是这种行为最近发生了变化,现在API不接受大于2147483647的值(即,2 ** 31 - 1 ).请参见此处

Originally, my hack passed Number.POSITIVE_INFINITY as the period, so the timer would actually never fire, but this behavior was recently changed and now the API doesn't accept anything greater than 2147483647 (i.e., 2 ** 31 - 1). See docs here and here.

作为参考,这是到目前为止给出的其他两个答案:

For reference, here are the other two answers given so far:

乔的:

require('net').createServer().listen();

将创建一个所谓的虚假监听器".一个较小的缺点是,我们仅为此分配了一个端口.

Will create a "bogus listener", as he called it. A minor downside is that we'd allocate a port just for that.

雅各布:

process.stdin.resume();

或等价物:

process.stdin.on("data", () => {});

stdin置于旧"模式,这是Node.js中仍然存在的不推荐使用的功能,用于与Node.js v0.10之前编写的脚本兼容(

Puts stdin into "old" mode, a deprecated feature that is still present in Node.js for compatibility with scripts written prior to Node.js v0.10 (reference).

这篇关于如何强行阻止Node.js进程终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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