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

Event-driven architecture 

Event-driven programming is a programming style in which code executes in response to an event, such as a keypress or a mouse click. In short, the flow of a program is driven by events.

GUI programming is an example of event-driven programming. For example, X Windows (driving most of your Linux GUI) processes a series of XEvents. Every keypress, mouse button press or release, and mouse movement generates a series of events. If you are on Linux, there is a command called xev. Running it via Terminal spawns a window. When moving a mouse over the window or pressing some keys, you can see the events that are generated.

Here is a capture of the xev program on my Linux laptop: 

You can plug in a callback, which gets triggered upon the reception of such an event. For example, an editor program could use keypress events to update its state (resulting in its documents being edited). Traditional event-driven programming could create a complex callback flow, thereby making it hard to figure out the control flows in the code.

Event-driven architecture (EDA) helps in decoupling a system's modules. Components communicate using events, which are encapsulated in messages. A component that emits an event does not know anything about the consumers. This makes EDA extremely loosely coupled. The architecture is inherently asynchronous. The producer is oblivious of the consumers of the event messages. This process is shown in the following diagram:

Given one thread and an event loop, with the callbacks executing quickly, we have a nice architecture. How does all this relate to concurrency? There could be multiple event loops running on a pool of threads. Thread pooling is an essential concept, as we will see in the upcoming chapters. 

As we have seen, an event loop manages events. The events are passed on to an installed handler, where they are processed. The handler can react to an event in two ways: either it succeeds or it fails. A failure is passed to the event loop again as another event. The handler for the exception decides to react accordingly. 

 

主站蜘蛛池模板: 太原市| 浑源县| 新民市| 兴隆县| 泰宁县| 察雅县| 临猗县| 息烽县| 资源县| 汽车| 西和县| 崇阳县| 杨浦区| 江永县| 晋州市| 新竹县| 武山县| 嘉善县| 澄迈县| 布尔津县| 葫芦岛市| 平南县| 朝阳市| 东乌珠穆沁旗| 甘南县| 任丘市| 高淳县| 娱乐| 蒙山县| 平原县| 南投市| 类乌齐县| 旌德县| 南宫市| 徐闻县| 清水县| 乌兰察布市| 浠水县| 南昌县| 根河市| 壤塘县|