在Node.js中,如果我正在编写一个长时间运行的函数,我应该使用setTimeout

时间:2022-09-04 08:17:47

or something else to queue up the rest of my function? and use callbacks or does node handle that automatically?

或其他什么来排队我的其余功能?并使用回调或节点自动处理?

I imagine that I would need to start my code and if there are other things that need to occur I should be giving up my functions control to give other events control. Is this the case? Or can i be stingy and node will cut off my function when I have used enough time?

我想我需要启动我的代码,如果还有其他事情需要发生,我应该放弃我的函数控制来给其他事件控制。是这样的吗?或者我可以吝啬,节点会在我用足够的时间后切断我的功能吗?

Thanks.

2 个解决方案

#1


5  

If your long-running function does a lot of I/O just make sure that you do this in a non-blocking way. This is how node.js achieves concurrency even though it only has a single thread: As soon as any task needs to wait for something, another task gets the CPU.

如果您的长时间运行功能确实执行了很多I / O,请确保以非阻塞方式执行此操作。这就是node.js实现并发的方式,即使它只有一个线程:只要任何任务需要等待某事,另一个任务就会获得CPU。

If your long-running function needs uninterrupted CPU time (or the I/O cannot be made asynchronously) , then you probably need to fork out a separate process, because otherwise every one else will have to wait until you are done.

如果长时间运行的函数需要不间断的CPU时间(或者不能异步进行I / O),那么您可能需要分出一个单独的进程,否则每个人都必须等到完成后再进行。

Or can i be stingy and node will cut off my function when I have used enough time?

或者我可以吝啬,节点会在我用足够的时间后切断我的功能吗?

No. This is totally cooperative multi-tasking. Node cannot preempt you.

不,这是完全合作的多任务处理。节点不能抢占你。

#2


3  

You should put your long running function or the code which takes long to execute into separate process because it can, for example, block other incoming requests while this code/function is executing. From node.js website:

您应该将长时间运行的函数或需要很长时间才能执行的代码放入单独的进程中,因为它可以在执行此代码/函数时阻止其他传入请求。来自node.js网站:

But what about multiple-processor concurrency? Aren't threads necessary to scale programs to multi-core computers? You can start new processes via child_process.fork() these other processes will be scheduled in parallel.

但是多处理器并发呢?将程序扩展到多核计算机不是必需的线程吗?您可以通过child_process.fork()启动新进程,这些其他进程将并行调度。

I would suggest to watch these articles/presentations in order to get a bigger picture on this topic:

我建议观看这些文章/演示文稿,以便对此主题进行更全面的了解:

#1


5  

If your long-running function does a lot of I/O just make sure that you do this in a non-blocking way. This is how node.js achieves concurrency even though it only has a single thread: As soon as any task needs to wait for something, another task gets the CPU.

如果您的长时间运行功能确实执行了很多I / O,请确保以非阻塞方式执行此操作。这就是node.js实现并发的方式,即使它只有一个线程:只要任何任务需要等待某事,另一个任务就会获得CPU。

If your long-running function needs uninterrupted CPU time (or the I/O cannot be made asynchronously) , then you probably need to fork out a separate process, because otherwise every one else will have to wait until you are done.

如果长时间运行的函数需要不间断的CPU时间(或者不能异步进行I / O),那么您可能需要分出一个单独的进程,否则每个人都必须等到完成后再进行。

Or can i be stingy and node will cut off my function when I have used enough time?

或者我可以吝啬,节点会在我用足够的时间后切断我的功能吗?

No. This is totally cooperative multi-tasking. Node cannot preempt you.

不,这是完全合作的多任务处理。节点不能抢占你。

#2


3  

You should put your long running function or the code which takes long to execute into separate process because it can, for example, block other incoming requests while this code/function is executing. From node.js website:

您应该将长时间运行的函数或需要很长时间才能执行的代码放入单独的进程中,因为它可以在执行此代码/函数时阻止其他传入请求。来自node.js网站:

But what about multiple-processor concurrency? Aren't threads necessary to scale programs to multi-core computers? You can start new processes via child_process.fork() these other processes will be scheduled in parallel.

但是多处理器并发呢?将程序扩展到多核计算机不是必需的线程吗?您可以通过child_process.fork()启动新进程,这些其他进程将并行调度。

I would suggest to watch these articles/presentations in order to get a bigger picture on this topic:

我建议观看这些文章/演示文稿,以便对此主题进行更全面的了解: