日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

How is javascript asynchronous AND single threaded?

發(fā)布時間:2024/4/13 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 How is javascript asynchronous AND single threaded? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文:?https://www.sohamkamani.com/blog/2016/03/14/wrapping-your-head-around-async-programming/

--------------------------------------------------------------------------------------------------------

How is javascript asynchronous AND single threaded? ?

Asynchronous programming is one of those programming paradigms that’s extremely difficult to fully understand, until you’ve done enough of it in practice. In an ideal world this shouldn’t be the case, so here’s yet another attempt to explain the concept of async programming, and why its different from parallel programming, in the context of javascript.

Everything runs on a different thread?except?our code.

At first glance, this sentence doesn’t seem to make a lot of sense. Isn’t everything we execute technically “our code”? Well, yes and no. Lets take a look at two examples of synchronous and asynchronous implementations of the same functionality.?Synchronous implementation (python) :

import requestsr = requests.get('http://sohamkamani.com') print r.text print "I come after the request"

Async implementation (js) :

var request = require('request'); request('http://sohamkamani.com', function (error, response, body) { console.log(body); }) console.log('I come after the request');

Now, all the above code runs on the same thread, no doubt about it. But what were missing is that the?request?and?requests?libraries, make http requests that go to other servers. The time spent in sending the request, processing it server side, and returning the response, is not spent in our thread. Thats what the web server you sent the request to does.
In our python implementation, we wait for all these processes to complete and receive the response before moving on to executing the next line of code. The async philosophy adopted by javascript and Node.js is fundamentally different in this regard. Instead of waiting for the response before executing the next bit of code, we declare what we?want?to happen once we receive our response, and move on to the next bit of code as usual.
This is why?"I come after the request"?will always get printed to the console after the response in the case of our python code, and always get printed before the response for our javascript code[1].

What good does any of this do me?

Both the snippets of code are exactly similar in their functionality :

  • import requests?==?var request = require('request');
  • r = requests.get('http://sohamkamani.com')?==?request('http://sohamkamani.com', ... )
  • print r.text?==?console.log(body);
  • print "I come after the request"?==?console.log('I come after the request');
  • Let us assume, for the sake of experimentation, that each of the 4 snippets of code above take ~10ms to execute. Since we are only here to see the power of async, we are not going to take the raw execution speed of either language into consideration, and assuming the synchronous parts of both examples to have the same execution time (of 10ms). We will also take two cases of waiting time into consideration, one of 20ms, and one of 5ms.

    Case 1 (Waiting time = 20ms)

    With synchronous execution :

    With asynchronous execution :

    Snippet 4 doesn’t have to wait for our response to arrive in order to execute, but snippet 3 does. Our javascript code handles this by defining tasks that need to wait inside the callback and other tasks outside of it. In the case of our python example, all code after we send the request is blocked until the response arrives.
    This gives us a?net loss of?10ms?in this case for the synchronous implementation.

    Case 2 (Waiting time = 5ms)

    With synchronous execution :

    With asynchronous execution :

    Synchronous execution with a smaller waiting time doesn’t look much different from the last picture, but the asynchronous timing diagram is pretty interesting. We see that snippet 4 starts execution as usual during waiting time, but snippet 3 doesnt execute right after the waiting time is over.?This is because snippet 3 and snippet 4 are running on the same threadand hence snippet 3 has to wait for 4 to finish before it can start. This is a much better illustration of what it means to be?single threaded and asynchronous.

    Final thoughts

    If async is so obviously the correct thing to do, then why should we bother with synchronous programming?

    The first thing that stands out in the javascript code snippet is that it’s?much less simple?than the corresponding python snippet, and so takes a bit more time to read, understand, and develop. In fact, there are many articles and?blog posts?dedicated to managing async code, because without proper management, it can all get out of hand pretty quickly.

    For rapid prototypes, or in cases where speed and timing is not the main concern, going the synchronous way can be more productive. On the other hand, if you’re planning to build an application with a lot of I/O and networking tasks, or with a lot of users, then the power of async really starts to shine.

    Although async is not embedded in pythons “philosophy”, like it is with NodeJs, there are many libraries which let you leverage event driven and async programming, like the?Twistedlibrary.

    總結(jié)

    以上是生活随笔為你收集整理的How is javascript asynchronous AND single threaded?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。