同步代码和异步代码_异步与同步代码

同步代码和异步代码

You might have heard that Node.js is fast because it provide asynchronous APIs for all expensive operations, like network access or filesystem.

您可能已经听说Node.js很快,因为它为所有昂贵的操作(如网络访问或文件系统)提供了异步API。

What does having an asynchronous API mean?

拥有异步API是什么意思?

If you anticipate an operation can take a lot of time, it makes sense to run it asynchronously, so other code can run in the meantime, and have a hook that’s called when that operation ends.

如果您预计某个操作可能要花费大量时间,则可以异步运行它,因此其他代码可以同时运行,并且在该操作结束时会调用一个钩子。

This is how Node.js can handle a lot more traffic than, say, PHP or Rails without using async libraries.

这样,Node.js可以在不使用异步库的情况下处理比PHP或Rails更多的流量。

Most programming languages that were not traditionally async today do have 3rd party libraries that implement ways to call asynchronous code.

如今,大多数传统上不是异步的编程语言都具有第三方库,这些库实现了调用异步代码的方式。

Otherwise what usually happens for example in PHP or Python code is that the thread blocks until the sync operation (reading from the network, writing a file..) ends.

否则,例如在PHP或Python代码中通常发生的事情是线程阻塞,直到同步操作(从网络读取,写入文件..)结束为止。

If the code runs asynchronously, the CPU is not idle waiting for the process to complete, but it can go on with other tasks queued up until the original process is ready to move on.

如果代码异步运行,则CPU不会空闲以等待进程完成,但是它可以继续处理其他任务,直到原始进程准备就绪。

翻译自: https://flaviocopes.com/async-vs-sync/

同步代码和异步代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值