Overview
The asyncio module in Python has built-in support for asynchronous IO and is used to handle asynchronous IO; it is a standard library introduced in Python version 3.4.
The programming model of asyncio is a message loop.
We directly get an EventLoop reference from the asyncio block, and then throw the coroutine that needs to be executed into the EventLoop to execute, which implements asynchronous IO.
Implement Hello world with asyncio
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2019/1/9 11:23 # @Author : Arrow and Bullet # @FileName: # @Software: PyCharm # @Blog :/qq_41800366 import asyncio @ def hello(): print("Hello world!") # Asynchronous call (2): yield from (2) print("Hello again!") # Get EventLoop:loop = asyncio.get_event_loop() # Execute coroutineloop.run_until_complete(hello()) ()
@ Mark a generator as coroutine type, and then we throw this coroutine into EventLoop to execute.
hello() will print out Hello world! first, and then the yield from syntax allows us to easily call another generator. Since () is also a coroutine, the thread does not wait for () , but directly interrupts and executes the next message loop.
When () returns, the thread can get the return value from yield from (here is None) and then execute the next line of statement.
(2) is regarded as an IO operation that takes 2 seconds (such as reading a large file). During this period, the main thread does not wait, but executes other executable coroutines in EventLoop, so concurrent execution can be achieved.
Let's try to encapsulate two coroutines using task:
import threading import asyncio @ def hello(): print('Hello world! (%s)' % ()) yield from (2) print('Hello again! (%s)' % ()) loop = asyncio.get_event_loop() tasks = [hello(), hello()] loop.run_until_complete((tasks)) ()
Observe the execution process:
Hello world! (<_MainThread(MainThread, started 140735195337472)>)
Hello world! (<_MainThread(MainThread, started 140735195337472)>)
(Pause for about 2 seconds)
Hello again! (<_MainThread(MainThread, started 140735195337472)>)
Hello again! (<_MainThread(MainThread, started 140735195337472)>)
As can be seen from the printed current thread name, the two coroutines are executed concurrently by the same thread.
If () is replaced with a real IO operation, multiple coroutines can be executed concurrently by one thread.
We use asyncio's asynchronous network connection to obtain the homepage of sina, sohu and 163:
import asyncio @ def wget(host): print('wget %s...' % host) connect = asyncio.open_connection(host, 80) # Create a connection reader, writer = yield from connect header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host (('utf-8')) yield from () while True: line = yield from () if line == b'\r\n': break print('%s header > %s' % (host, ('utf-8').rstrip())) # Ignore the body, close the socket () loop = asyncio.get_event_loop() tasks = [wget(host) for host in ['', '', '']] loop.run_until_complete((tasks)) ()
The execution results are as follows:
wget ...
wget ...
wget ...
(Wait for a while)
(Print out the header of sohu)
header > HTTP/1.1 200 OK
header > Content-Type: text/html
...
(Print out the header of sina)
header > HTTP/1.1 200 OK
header > Date: Wed, 20 May 2015 04:56:33 GMT
...
(Print out the header of 163)
header > HTTP/1.0 302 Moved Temporarily
header > Server: Cdn Cache Server V2.0
...
It can be seen that 3 connections are completed concurrently by one thread through coroutine.
Summarize
asyncio provides complete asynchronous IO support;
Asynchronous operations need to be completed through yield from in coroutine;
Multiple coroutines can be encapsulated into a set of tasks and then executed concurrently.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.