- Concurrent Patterns and Best Practices
- Atul S. Khot
- 367字
- 2021-07-16 17:32:31
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.
- Linux實戰
- Linux從零開始學(視頻教學版)
- Haskell Financial Data Modeling and Predictive Analytics
- SharePoint 2013 應用開發實戰
- Microsoft Operations Management Suite Cookbook
- 嵌入式實時操作系統μC/OS原理與實踐
- 一學就會:Windows Vista應用完全自學手冊
- NetDevOps入門與實踐
- 計算機系統的自主設計
- Kali Linux高級滲透測試(原書第3版)
- Linux 從入門到項目實踐(超值版)
- Gradle Effective Implementations Guide(Second Edition)
- Administering ArcGIS for Server
- 鴻蒙應用開發實戰
- 嵌入式Linux設備驅動程序開發指南(原書第2版)