日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 美女日批视频在线观看 | 一级黄色性视频 | 中文字幕免费在线看线人 | 亚洲国产婷婷香蕉久久久久久99 | 青青青在线免费 | 五月激情啪啪 | 亚洲国产成人综合 | av小片| 91丝袜呻吟高潮美腿白嫩在线观看 | 五月天堂婷婷 | 蜜桃成人在线 | 99无码熟妇丰满人妻啪啪 | 婷婷精品一区二区三区 | v在线| 日韩精品中字 | 国产毛片毛片毛片 | 国产网站黄| 成人做爰www免费看视频网站 | 免费麻豆av | 九九久久精品 | 9999在线视频| 97福利在线 | 免费中文字幕 | 亚洲精品久久 | 天堂网亚洲 | 亚色视频在线观看 | 欧美亚洲精品在线观看 | 国产黄色在线看 | 东京干手机福利视频 | 久草久操 | 一本色道久久综合狠狠躁的推荐 | 国产熟女一区二区丰满 | 嫩草嫩草嫩草嫩草嫩草 | 亚洲av男人的天堂在线观看 | 欧美一区二区在线观看视频 | 欧美123区 | 久久不射电影网 | 91精品国产色综合久久不卡粉嫩 | 成人毛片在线视频 | 午夜黄色影院 | 国产一级做a爰片久久毛片男男 | 国产精品入口66mio男同 | 91热久久| 亚洲综合视频在线播放 | 性史性农村dvd毛片 日韩精品在线视频观看 | 日韩精品一区二区三区不卡 | 韩国甜性涩爱 | 精品二区视频 | 激情第四色 | 末路1997全集免费观看完整版 | 日本免费一区二区三区四区五六区 | 抽插丰满内射高潮视频 | 国产在线观看第一页 | 久久99免费视频 | 免费成人深夜夜行p站 | 午夜电影在线播放 | 93久久精品日日躁夜夜躁欧美 | 一级少妇女片 | 亚洲精品久久久久久宅男 | 欧美激情小视频 | 国产成人一区二区三区小说 | 久久视频热| 国产自产自拍 | 伊人久久大香网 | 天天射天天 | 伊人三区| 免费在线观看国产精品 | 人妻色综合网站 | 成人在线观看av | 日批免费网站 | 久操av | 男人的天堂视频在线观看 | 欧美熟妇精品黑人巨大一二三区 | 日韩激情网 | 黄网在线 | 国产大片b站| 香蕉久久精品日日躁夜夜躁 | 一区二区三区av夏目彩春 | 精品免费国产 | 黄色免费网页 | 经典三级第一页 | 午夜一区二区三区在线 | 麻豆av在线播放 | 老女人做爰全过程免费的视频 | av天堂一区二区三区 | 久久久一二三区 | 在线看黄色网址 | 最新黄色网址在线观看 | 中文写幕一区二区三区免费观成熟 | 日产久久视频 | 99精品一区二区三区 | 人妻激情偷乱频一区二区三区 | 欧美浓毛大泬视频 | 亚洲精品久久久久久无码色欲四季 | 中国免费看的片 | 尤物在线视频观看 | 久久久久久中文字幕 | 日本一区二区免费高清视频 | 五月天综合婷婷 |