简体   繁体   English

异步的真正工作原理以及如何与node.js正确使用(node-webkit)

[英]How Async really works and How to use it properly with node.js (node-webkit)

efor this problem i am using Node-Webkit (node.js) and Async , loading a Windows App. 对于这个问题,我正在使用Node-Webkit (node.js)和Async ,正在加载Windows应用程序。

The reason of this question is to definitively answer: 这个问题的原因是要明确回答:

What really means asynchronous execution in Javascript and Node.Js. 在Javascript和Node.Js中真正意味着异步执行。

My personal code problem is at the end of the Question. 我的个人代码问题在问题的末尾。 "The Case". “案子”。

I am going to explain all about the problem i have directly with a schematic summary. 我将直接用原理图摘要解释所有有关该问题的信息。 (And I will be updating the info as you help me to understand it) (而且我会在您帮助我了解信息时对其进行更新)


The Concept (theory) 概念(理论)

Imagine a Primary Screen (JS, Html, css, ... Node.js frameworks) and a Background Procedure (JS execution every 10 min, JS internal checks, background Database Optimization, ...). 想象一下一个主屏幕(JS,Html,css,... Node.js框架)和一个后台过程(每10分钟执行一次JS,JS内部检查,后台数据库优化等)。

Whatever you do in Primary Screen wont affect background execution (except some important cases), and Background can change even the Screen if he needs to (screen timers, info about online web status, ...) 您在“主屏幕”中所做的任何操作都不会影响后台执行(某些重要情况除外),并且如果他需要,后台甚至可以更改屏幕(屏幕计时器,有关在线网络状态的信息,等等)。

Then the behaviour is like: 那么行为就像:

Thread 1: Your actions inside the App framework. 线程1: 您在App框架内的操作。 Thread 2: Background App routines 线程2: 后台应用程序

Any action as they finish gives his output to screen, despite of the rest of the actions in async parallel 尽管完成了异步并行中的其余操作,但完成的任何操作都会将其输出显示在屏幕上


The Interpretation (For me) 解释(对我来说)

I think this is something that "Async" will handle without problems, as a parallel execution. 我认为这是“异步”将作为并行执行毫无问题地处理的事情。

async.parallel([
function(){ ... },
function(){ ... }
], callback);      //optional callback

So the Thread 1 and Thread 2 can work together correctly while they do not affect the same code or instruction. 因此,线程1和线程2可以在不影响相同代码或指令的情况下正确地协同工作。 The Content will be changing while any threads request something of/to it. 当任何线程向其请求某些内容时,内容将发生变化。


The Implementation (Reality) Code is not fully asynchronous during the execution, there are sync parts with common actions, that when they need calls the async codes. 实施(Reality)代码在执行期间不是完全异步的,其中有些同步部分具有常见的操作,需要在需要时调用异步代码。

Sync: Startup with containers -> Async: load multiple content and do general stuff -> Sync: Do an action in the screen -> ... 同步:使用容器启动->异步:加载多个内容并执行常规操作->同步:在屏幕上执行操作-> ...


The Case 案子

So here it is my not working properly code: 所以这是我无法正常工作的代码:

win.on('loaded', function() {

 $( "#ContentProgram" ).load( "view/launcherWorkSpace.html", function() {
$("#bgLauncher").hide();
win.show();

async.parallel([
    function()   //**Background Process: Access to DB and return HTML content**
    {       
            var datacontent = new data.GetActiveData();  
        var exeSQL = new data.conn(datacontent);
            if(exeSQL.Res) 
            {
                var r = exeSQL.Content;
                if(r.Found)
                {
                    logSalon = new data.activeSData(r)
                    $('#RelativeInfo').empty();
                    $("#RelativeInfo").html("<h4 class='text-success'>Data found: <b>" + logData.getName + "</b></h4>");
                }
            }
    },
    function()  //**Foreground Process: See an effect on screen during load.**
    {
        $("#bgLauncher").fadeIn(400);
        $("#centralAccess").delay(500).animate({bottom:0},200);
    }
]);
});
});

As you can see, im not using "Callback()" because i dont need to (and it does the same). 如您所见,即时通讯不使用“ Callback()”,因为我不需要(和它一样)。

I want to do the Foreground Process even if Background Process is not finished, but the result of the code is done at same time when both request has finished... 即使后台进程尚未完成,我也想执行前景进程,但是代码的结果是在两个请求都完成的同时完成的。

If i disconect the DB manually, first function takes 3 seconds until gives an exception (that i wont handle). 如果我手动断开数据库,第一个功能将花费3秒钟,直到出现异常(我不会处理)。 Until then, both proccess will not output (show on screen) anything. 在此之前,两个过程都不​​会输出任何东西(显示在屏幕上)。 (Foreground Process should be launched whatever happends to Background Process). (无论后台进程发生什么,都应该启动前台进程)。

Thanks and sorry for so much explanation for something that looks like trivial. 感谢和抱歉,为如此琐碎的事物提供了太多解释。


EDITED EDITED

This start to be annoying... I tried without Async, just a javascript with callback like this: 这开始很烦人。。。我试过没有异步,只是一个带有回调的JavaScript,像这样:

            launchEffect(function () {
            var datacontent = new data.GetActiveData();

            var exeSQL = new data.conn(datacontent);

            if(exeSQL.Res) 
            {
                var r = exeSQL.Content;

                if(r.Found)
                {
                    logData = new data.activeData(r)
                    $('#RelativeInfo').empty();
                    $("#RelativeInfo").html("<h4 class='text-success'>Salón: <b>" + log.getName + "</b></h4>");
                }
            }
        });
    });

});
function launchEffect(callback)
{
  $("#bgLauncher").fadeIn(400);
    $("#centralAccess").delay(500).animate({bottom:0},200);
    callback();
}

Even with this... Jquery doesnt work until the callback answer... 即使这样... jQuery直到回调答案才起作用...

node-webkit let's you run code written like code for node.js, but is ultimately just a shim running in WebKit's Javascript runtime and only has one thread, which means that most 'asynchronous' code will still block the execution of any other code. node-webkit让您运行类似于node.js的代码,但最终只是在WebKit的Javascript运行时中运行的填充程序,并且只有一个线程,这意味着大多数“异步”代码仍将阻止其他任何代码的执行。

If you were running node.js itself, you'd see different behavior because it can do genuinely asynchronous threading behind the scenes. 如果您运行的是node.js本身,则会看到不同的行为,因为它可以在后台进行真正的异步线程处理。 If you want more threads, you'll need to supply them in your host app. 如果需要更多线程,则需要在主机应用程序中提供它们。

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

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