18. Week 7 - Network Programming - Event Driven Introduction

 

Introduction to the event-driven model

1. There are two traditional programming modes as follows:

A. The general process of linear mode
  Code structure:
    start--->
    code block A--->
    code block B--->
    code block C--->
    code block D--->
    ......
    -- -> end

  Understanding the process: Each code block is code that accomplishes various things, but the programmer knows the execution order of code blocks A, B, C, D... The only thing that can change this process is data. Enter different data and judge according to the conditional statement, the process may be changed to A--->C--->E...--->End. The order in which the program runs may be different each time, but its control flow is determined by the input data and the program you write. If you know the current running state of the program (including the input data and the program itself), then you know the next and even until the end of its running process.

B. The general process of the event-driven program model
  Code structure:
    start --->
    initialization --->
    --> wait

  Understanding the process: Unlike the traditional programming mode above, the event-driven program waits there after it is started. What are you waiting for? Waiting to be triggered by an event. There are also "waiting" times in traditional programming. For example, in code block D, you define an input() that requires the user to input data. But this is different from the following waiting, the "waiting" of traditional programming, such as input(), you as a program writer know or force the user to input something, perhaps a number, or a file name. If the user enters a wrong input, You also need to remind him and ask him to re-enter. Event-driven waits are completely unaware and do not force user input or anything. As long as an event occurs, the program will make a corresponding "reaction". These events include: input information, mouse, hitting a key on the keyboard and the system's internal timer trigger.

 

2. Event-driven model

When writing a program for a server to process a model, there are the following models:

(1) Every time a request is received, a new process is created to process the request;
(2) Every time a request is received, a new thread is created to process the request;
(3) Each time a request is received, an event list is put into it, so that the main process processes the request through non-blocking I/O

 The advantages and disadvantages of the above methods are compared:

  • The method in (1), because the overhead of creating a new process is relatively large, so it will lead to poor server performance, but the implementation is relatively simple.
  • The (2) method may face deadlock and other problems due to the synchronization of threads involved.
  • The (3) way, when writing application code, the logic is more complicated than the previous two.

Considering various factors, it is generally believed that the (3) method is the method adopted by most network servers.

 

3. Coroutine event-driven method: It is generally believed that this method is the method adopted by most network servers  

For example: If in UI programming, it is often necessary to respond to mouse clicks, how to obtain mouse clicks in the first place?

Method 1: Create a thread that loops to detect whether there is a mouse click, then this method has the following disadvantages:

  • CPU resources are wasted, and the frequency of mouse clicks may be very small, but the scanning thread will continue to loop detection, which will cause a lot of waste of CPU resources; what if the interface for scanning mouse clicks is blocked?
  • If it is blocked, the following problems will occur. If we not only scan for mouse clicks, but also scan whether the keyboard is pressed, because the scanning of the mouse is blocked, the keyboard may never be scanned;
  • If a cycle needs to scan a lot of devices, this will lead to response time problems;

Therefore, this method is very bad.

Method 2: Event-driven model

 At present, most UI programming is an event-driven model. For example, many UI platforms provide the onClick() event, which represents the mouse press event. The general idea of ​​the event-driven model is as follows:

  • There is an event (message) queue;
  • When the mouse is pressed, add a click event (message) to this queue;
  • There is a loop that continuously takes events from the queue, and calls different functions according to different events, such as onClick(), onKeyDown(), etc.;
  • Events (messages) generally save their own handler function pointers, so that each message has an independent handler function;

  Event-driven programming is a programming paradigm where the flow of execution of a program is determined by external events. It features an event loop that uses a callback mechanism to trigger corresponding processing when external events occur. Two other common programming paradigms are (single-threaded) synchronization and multi-threaded programming.

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694483&siteId=291194637