1. What is coroutine
Coroutine, the first thing I saw in Lua , Coroutine's biggest benefit is that the stack can be saved, the program will continue to execute, and in Python , it's generally using yield to achieve, the specific can see the following articles :
Http://www.cnblogs.com/tqsummer/archive/2010/12/27/1917927.html
the yield and yield from syntax in Python allows the program to support coroutine
2.Asyncio Library
Python3 , a coroutine -based Asynchronous IO Library is provided, which is Asyncio
Https://docs.python.org/3/library/asyncio.html
2.1 Event Loop
Asyncio Library An important concept is the event loop, only after the event loop is started, you can allow the coroutine task to continue execution, if the event loop stops or pauses, then the entire asynchronous IO Also stops or pauses, similar to the operating system's event loop mechanism
AsyncioThe interior is based onSelector may also be epoll) or windows iocp This is why you need to start a Span style= "font-family: ' Microsoft Yahei ';" lang= "en-US" >event loop,event loopio "for each platform wait " Encapsulation of this operation
2.2 Asyncio library
Cotoutine
Coroutine is a function written in support, which can be used to determine whether a function is coroutine , which requires a @asyncio. coroutine to decorate
Use@asyncio. coroutineAfter the modification, this function can supportawait ( Python 3.5) or yield From syntax , once yield from syntax, asyncio will suspend the current coroutine to perform other coroutine
The following code :
After you start to run the event loop
1. start execution of sleep3s
2. when the program starts to sleep, theevent loop does not stop the current thread, but suspends the current function, executing the next coroutine, which is sleep5s
3.sleep5s starts to sleep, hangs the current function
4.event Loop detects that the sleep 3s time has arrived , and then re-executes the suspended sleep3s,sleep3s execution
5.sleep5s Time has arrived , so the execution of the suspended sleep5s,sleep5s is complete .
Coroutine-based servers
For example,_tcp_listen creates a TCP server and receives a TCP link, creates a _tcp_recv task, and then starts listening data for TCP connections
differs from callback-based libraries
Coroutine version Code on recv the logical processing code is linear rather than broken, callback-based code often needs to jump , readability is high
And instead of creating temporary data to save some variables, the self.tcp_clients is because the old version is callback-based and requires the data member to store it (too lazy to re-write the code, which is the code to refactor a tool to a normal time.) )
Extended Asyncio
Because the project needs to know the address of each other 's UDP, it is necessary to extend the sock_recv, using the recv_from provided by the socket itself
Look at the source of the next loop.socket_recv ,loop.socket_recv
The socket must be set to non-blocking before calling, or an error will be
The key point isAdd_reader This function will socket routed to async io ( default is selector) and send a callback in Span style= "font-family: ' Microsoft yahei ';" lang= "en-US" > ( _sock_recv self this function, as long as the data can be read, will again call the Span style= "font-family: ' Microsoft Yahei ';" lang= "en-US" >_sock_recv this function
Asyncio also supports not only sleep and io operations, but also multi-process,lock and other operations
As long as data = SOCK.RECV (n) is changed to data, addr = Sock.recv_from (n) is available.
In 3.5 , the new async and await syntax is added to replace the asynio.coroutine and yield from
Python3.5 's async and await
https://www.python.org/dev/peps/pep-0492/
Python Asyncio Notes