Event-driven models
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Title</title>6 7 </Head>8 <Body>9 Ten <Ponclick= "Fun ()">Dot me!</P> One A - <Scripttype= "Text/javascript"> - functionFun () { the Alert ('about it?') - } - </Script> - </Body> + - </HTML> + AEvent-driven Mouse click event Registration
Event-driven mouse events
The traditional programming is in the following linear mode:
Start---> code block a---> code block b---> block c---> Block d---...---> End
Each code block is a code that accomplishes a variety of things, but the programmer knows the code block a,b,c,d ... Order of execution, the only thing that can change this process is data. Enter different data, judging by the conditional statement, the process may be changed to a--->c--->e ...---> End. Each time the program may run in a different order, but its control flow is determined by the input data and the program you are writing. If you know the current running state of the program (including the input data and the program itself), then you know that the next step is even until the end of its running process.
For the event-driven program model, the process is roughly as follows:
Start---> Initialize---> Wait
Unlike the traditional programming model above, the event driver is waiting there after startup, waiting for what? Wait for the event to be triggered. Traditional programming also has a "wait" time, such as in code block D, you define an input (), the user needs to enter data. But this is different from the wait for the following, traditional programming "Wait", such as input (), you as a program writer know or force the user to enter something, perhaps the number, perhaps the file name, if the user input error, you need to remind him, and ask him to re-enter. The wait for the event driver is completely unknown and does not force the user to enter or do anything. As long as an event occurs, the program will respond accordingly. These events include input information, a mouse, a key on the keyboard, and a system internal timer trigger.
Most of the current UI programming is an event-driven model, as many UI platforms provide the OnClick () event, which represents the mouse down event. The event-driven model is broadly thought of 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 constantly takes events out of the queue, and calls different functions, such as onclick (), OnKeyDown (), according to different events;
- Events (messages) typically hold their own handler pointers, so that each message has its own handler function;
Event-driven programming is a programming paradigm where the execution flow of a program is determined by an external event. It is characterized by the inclusion of an event loop that uses a callback mechanism to trigger the corresponding processing when an external event occurs .
Let's use examples to compare and contrast single-threaded, multithreaded, and event-driven programming models. Shows the work done by the programs in these three modes over time. This program has 3 tasks to complete, each of which blocks itself while waiting for an I/O operation. The time it takes to block the I/O operation has been marked with a gray box.
Four IO Models
Blocking io (blocking IO)
Non-blocking io (non-blocking IO)
IO multiplexing (IO multiplexing)
asynchronous I/O (asynchronous IO)
Python asynchronous Io, io multiplexing