1. Concept:
Synchronous IO:
CPU speed is much faster than disk, network IO, IO operations during the event, such as read and write files, send network data, you need to wait for IO operation to complete before continuing to the next step. This condition is called synchronous IO.
IO operation when the current thread is suspended, other code executed by the CPU need not execute.
To achieve concurrency must be multi-threaded / multi-process or asynchronous IO,
Multi-thread / process shortcomings are as follows:
- No system can not increase the upper limit of the thread (in other words, the disadvantage is limited by system resources),
- System switching overhead increases with the number of threads increases, increasing the switching takes time, cpu reduce the execution time, task completion efficiency drops
Asynchronous IO:
"When an IO operation code requires a time-consuming, it is only issued IO instruction, IO does not wait for the result, and then went to execute other code. After a while, when the IO return result, and then notify the CPU for processing."
2. Implement
Asynchronous IO model requires a message loop, in a message loop, the main thread continuously repeating "read message - the message handling" process:
- When faced with IO operation, code only responsible for issuing IO request, without waiting for the results of IO, and then ends the current round of message processing, message processing to the next round. After the completion of IO operation, will receive a "IO completed" message, can obtain the operation result directly IO processing this message.
- In the "IO request issued" this time to receive the "IO completion", the
Synchronous IO model , the main thread can only be suspended, but asynchronous IO model, the main thread does not break, but continues to process other messages in the message loop. This
Asynchronous IO model , a thread can simultaneously handle a plurality of IO requests, and the operation does not switch threads. For most IO intensive applications, the use of asynchronous IO will greatly enhance the multi-tasking capabilities of the system.
3. coroutine / micro-threaded / shred Coroutine
Subroutine
Also known as sub-function, level call stack implementation, a thread executes a subroutine. The first call, a return, a clear call to order.
Coroutine
Similar routines, differences are as follows:
- Coroutine execution process may be interrupted within the subroutine, then process goes to other routines at the appropriate time and back again is then performed
- Executed by a thread, not the thread switch
- Do not need multi-threaded locking mechanism (because only one thread, there is no conflict while writing variable, control of the shared resource is not locked in coroutine, just like a judge status, so the efficiency is much higher than the multi-threading)
Coroutine and multi-core CPU: multi-process + coroutine
Realization (Python) generator
EG: Producer Consumer
4.asyncio
asyncio standard library Python 3.4 version introduced directly built-in support for asynchronous IO's.
asyncio programming model is a message loop.
We get directly from asyncio module EventLoop a reference, then you need to perform coroutine thrown EventLoop executed, on the realization of asynchronous IO.
EG: Hello World
Simplify the identification of asynchronous IO: async and await a new syntax for the coroutine
1. @ asyncio.coroutine replaced with the async;
2. Replace the yield from await.
5.aiohttp
asyncio can achieve single-threaded concurrent IO operations. If only the client, not the power play. If asyncio used on the server side, such as Web servers, since an HTTP connection is IO operations, it is possible to achieve high single-threaded concurrent + coroutine support multiple users.
asyncio implements TCP, UDP, SSL and other protocols, aiohttp framework is based on HTTP asyncio achieved.