javascript javascript中函数的并行执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23939290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:51:08  来源:igfitidea点击:

Parallel execution of functions in javascript

javascriptjqueryparallel-processing

提问by user3422501

I listened that javascript is single threaded am I right? Then how can I implement execution of functions(multiple) in parallel(simultaneous).

我听说 javascript 是单线程的,对吗?那么如何并行(同时)实现函数(多个)的执行。

I have script as shown below, and I want to tell you that each xml file size is 4.6MB.

我有如下所示的脚本,我想告诉你每个xml文件大小是4.6MB。

myfunction('sample1.xml');
myfunction('sample2.xml');
myfunction('sample3.xml');
    .
    .
myfunction('sample15.xml');

function myfunction(name) {

    $.ajax({
        type: "GET",
        url: name,
        dataType: "xml",
        success: parseXml
    });

    function parseXml(xml) {

        //searching the xml file 


    }

}

My aim is to speed up the process of searching xml file, for that I have thought that parallel execution is good. So is it possible to have parallel execution in javascript functions or is their any way to speed up my functions execution.

我的目标是加快搜索 xml 文件的过程,为此我认为并行执行是好的。那么是否可以在 javascript 函数中进行并行执行,或者它们是否可以通过任何方式来加速我的函数执行。

回答by ffflabs

You can trigger as many ajax request as you want and they will fire in parallel because they are asynchronous by default. The problem is that your browser will execute parseXML whenever the ajax request is ready, so you might end freezing your browser anyway.

您可以根据需要触发任意数量的 ajax 请求,它们将并行触发,因为默认情况下它们是异步的。问题在于,只要 ajax 请求准备就绪,您的浏览器就会执行 parseXML,因此无论如何您都可能会冻结浏览器。

You can defer the execution of parseXML with Mark Gabriel's response

您可以使用 Mark Gabriel 的响应推迟 parseXML 的执行

setTimeout(function(){ parseXML(xml) }, 0)

Which would prevent browser freezing, but in the end would execute parseXML sequentially.

这将防止浏览器冻结,但最终会按顺序执行 parseXML。

Depending on what exactly are you trying to do inside parseXML, it might be better to use webworkers to execute the XML parsing in a parallel browser process. This way you will be opening a background process to perform a specific task. You create the worker and send the filename as a message, then wait for the worker to return whatever you are expecting from parseXML

根据您在 parseXML 中到底要做什么,最好使用 webworkers 在并行浏览器进程中执行 XML 解析。通过这种方式,您将打开一个后台进程来执行特定任务。您创建工作程序并将文件名作为消息发送,然后等待工作程序从 parseXML 返回您期望的任何内容

var worker = new Worker('/js/parseXML.js');
worker.addEventListener('message', function (e) {
        console.log('Webworker answer is',e);
    }, false);

worker.postMessage('sample1.xml'); // Send data to our worker.

the contents of parseXML.js would be

parseXML.js 的内容是

importScripts("/js/jquery.js");

self.addEventListener('message', function (e) {
    console.log('Webworker received', e.data);
    $.ajax({
        type: "GET",
        url: e.data,
        dataType: "xml",
        success: parseXml
    });

    function parseXml(xml) {
        //searching the xml file 
        self.postMessage(parsedXML);
    };
}, false);

Please keep in mind that this logic only makes sense if you are planning to get a string, array or hash as the return of parseXML. You can't operate on global objects of the main script inside a webworker nor return complex objects.

请记住,只有当您计划获取字符串、数组或散列作为 parseXML 的返回值时,此逻辑才有意义。您不能在 webworker 内部对主脚本的全局对象进行操作,也不能返回复杂的对象。