简体   繁体   English

无法在函数内部调用非内置函数

[英]Can't call non built-in function inside a function

I have issue in my codes - there's a part in my codes make much calculations (30~40 seconds) 我的代码有问题-我的代码中有一部分需要进行大量计算(30到40秒)

I need to reduce this time as much as possible - so I have to make use of my all CPU threads and distribute this part to be done more fast 我需要尽可能减少这种时间-因此,我必须利用我所有的CPU线程并分发此部分,以使其更快地完成。

for a few days - I made much researches and googled enough about how to write a multi-threaded code in nodejs , and I passed on many opinions about how nodejs is single threaded or multi-threaded , and readed about some topics like clustering , child-processes , and workers .. 几天的时间-我对如何在nodejs中编写多线程代码进行了很多研究,并进行了足够的搜索,并且就如何将nodejs单线程或多线程传递了很多意见,并阅读了一些主题,例如clustering,child -过程和工人..

I prefered to use a ready library to not dig into these topics too much , so I tried this library : https://github.com/Microsoft/napajs , but it fired some issues with me. 我宁愿使用现成的库也不要过多地研究这些主题,所以我尝试了这个库: https : //github.com/Microsoft/napajs ,但它引发了我一些问题。

what I want should be simple - I just have array (ready for parallel computation - each thread ready to work on each element) [data,data,data,data] 我想要的应该是简单的-我只有数组(准备进行并行计算-每个线程准备在每个元素上工作)[数据,数据,数据,数据]

I found this library : https://www.npmjs.com/package/paralleljs a bit simple and has this method (map) which great for my case 我发现了这个库: https : //www.npmjs.com/package/paralleljs有点简单,并且具有此方法(映射),非常适合我的情况

let's move to my code 让我们转到我的代码

initializing 初始化

var p = new Parallel([54,25,66,23,14,27,15,18] , { maxWorkers : 8 });

3 functions (fun is the main function which call for exports.calc and calcc) 3个功能(乐趣是调用exports.calc和calcc的主要功能)

exports.calc = function(i,h)
{
    return Math.log2(i*h);
}

function calcc(i,h)
{
    return Math.log2(i*h);
}

function fun(h)
{
    var sum = 0;
   for (var i = 1 ; i < 55555 ; i++)
   {
       for (var l = 1 ; l < 55555 ; l++)
       {
           sum+=(exports.calc(i,l)); // in this case calc is not a function error
           sum+=(calcc(i,l)); // in this case calcc is not defined error
       }
   } 
   return sum;
}

starting the parallel calculations using map function - this function should divide the array and let each thread to work on each element separately 使用map函数开始并行计算-此函数应划分数组并让每个线程分别对每个元素进行处理

p.map(fun).then(data => {
        console.log(data);
    });

the error always occurs in fun - anytime trying to call non built-in functions - for example 错误总是很有趣-随时尝试调用非内置函数-例如

Math.max() , String.includes() and more ,, all these built-in functions are not making any issues , but any non built-in functions like calcc and exports.calc making issues Math.max(),String.includes()等等,所有这些内置函数都不会引起任何问题,但是任何非内置函数(例如calcc和exports.calc)都会引起问题

per the library docs , it uses the child processes - if this can help , 根据库文档,它使用子进程-如果这样做可以帮助,

if you can't help with this issue - but have other way to make this code possible (with any other libraries) - please share it 如果您不能解决此问题-但有其他方法可以使此代码成为可能(与任何其他库一起使用)-请共享

This can be implemented with Node cluster : 这可以通过Node cluster实现:

var cluster = require('cluster');

function calcc(i,h) {
    return Math.log2(i*h);
}

function fun(h) {
   var sum = 0;
   for (var i = 1 ; i < 5555 ; i++) {
       for (var l = 1 ; l < 5555 ; l++) {
           sum+=(calcc(i,l));
           sum+=(calcc(i,l));
       }
   } 
   return sum;
}

if (cluster.isMaster) {   
  var CPU = 8;    
  var count = 0;

  function messageHandler(msg) {
    console.log(msg);
    count++;
    if (count == CPU)
      console.log('Complete');
  }

  for (let i = 0; i < CPU; i++) {
    var worker = cluster.fork();
    worker.on('message', messageHandler);
  }    
} else if (cluster.isWorker) {
  process.send(fun());
}

Since a cluster is intended for network connections, it can provide some overhead, so low-level implementation with child_process.fork() (which is used by cluster ) could be more efficient. 由于群集旨在用于网络连接,因此它可以提供一些开销,因此使用child_process.fork() (由cluster使用child_process.fork()的低级实现可能会更高效。

And there are experimental Node worker threads for Node 10 and higher. 并且有针对Node 10及更高版本的实验性Node worker线程

Never used the parallel myself, but based on behaviour and how you use it, I suppose @estus is basically right. 我自己从未使用过并行,但基于行为和使用方式,我认为@estus基本上是正确的。

If the function itself is passed -> it does not bring with itself any information about previous surroundings. 如果函数本身被传递->它不会带来有关先前环境的任何信息。

You will need to initialize at the function beginning everything you use. 您将需要在函数处进行初始化,以开始使用的所有内容。

Most direct way is to just put it inside main function: 最直接的方法是将其放在主要函数中:

function fun(h)
{
    const calc = function(){...}
}

The most elegant is to use require inside. 最优雅的是在内部使用require。 So put everything you want to share into another module, export it and just do the 因此,将您要共享的所有内容放入另一个模块,将其导出,然后执行

function fun(h)
{
    const tools = require('tools');
    tools.calc(...)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM