官术网_书友最值得收藏!

Asynchronous programming

The term asynchronous programming is very common in languages such as JavaScript. A very general definition is that, in asynchronous programming, we're executing code in a different order than it was defined. This is typical for any event based application.

For example, in JavaScript, we first define an event listener with its handler, which is executed some time later, when an appropriate event occurs.

In PHP, this could be, for example, a web application that needs to send an e-mail when we create a new blog article. Just, instead of lines of code, we're considering tasks. The following figure demonstrates an asynchronously triggered event:

While the web application was saving an article (processing a task), it triggered an event that sent an e-mail and then carried on with the original task. The event handler had to be defined somewhere before we started this task.

Asynchronous versus parallel programming

A very common misconception is that asynchronous and parallel programming are the same, or that one is an implication of the other. This is very common in JavaScript where, from the user's perspective, it looks like things are running in parallel.

This isn't true, but many programming languages (in fact, just their interpreters), create the illusion of running in parallel while they're still sequential. They appear to be parallel due to it's event-based nature (JavaScript), or because of their interpreter internals.

For example, Python simulates threads by switching the execution context between different parts of the application. The Python interpreter is still single threaded and executes instructions sequentially, but creates the illusion of running code in parallel.

主站蜘蛛池模板: 万宁市| 峨眉山市| 辽阳县| 黑水县| 永善县| 自治县| 长沙县| 衢州市| 黑龙江省| 宁阳县| 太仓市| 太仆寺旗| 陕西省| 阳东县| 繁峙县| 丰宁| 庆阳市| 东莞市| 延边| 霍州市| 保靖县| 阿城市| 万盛区| 安顺市| 扎兰屯市| 弋阳县| 儋州市| 海晏县| 陆良县| 谷城县| 临夏县| 屏东县| 繁峙县| 房山区| 乡宁县| 中超| 商南县| 班玛县| 堆龙德庆县| 高清| 阿克|