The python asynchronous IO

  We know, CPU speed is much faster than disk, network, and other IO. In one thread, CPU speed is extremely fast code execution, however, in the event of IO operations, 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.

 

  During the IO operation, the current thread is suspended, while others need the code can not be executed by the CPU executing the current thread.

  Because an IO operation on the current thread is blocked, causing other code can not execute, so we have to use multiple threads or multiple processes concurrently execute code, for multiple users. Each user is assigned a thread, if you encounter leads to IO thread is suspended, other user threads are not affected.

  Multi-threaded and multi-process model solves the concurrency problem, but the system can not be increased without limit thread. Since the thread switching overhead is also great, so once an excessive number of threads, CPU time is spent on a thread switch on a real time running the code less, resulting in severe performance degradation.

  Since we want to solve the problem is to perform a high-speed CPU and IO devices turtle speed capability of a serious mismatch, multi-threaded and multi-process is just a way to solve this problem.

  Another way to solve the problem is asynchronous IO IO. When the code needs to perform a time-consuming operation IO, IO command is only issued without waiting for the results of IO, and then went to execute other code. After some time, when the IO return result, and then notify the CPU for processing.

 

 

1) aiohttp

asyncioYou can achieve single-threaded concurrent IO operations. If only the client, not the power play. If asyncioused on the server, such as Web servers, since an HTTP connection is IO operations, can be single-threaded + coroutinehigh concurrency support multiple users.

asyncioImplements TCP, UDP, SSL and other protocols, aiohttpis based on asyncioHTTP framework implementation.

  installation: 

pip install aiohttp

  Example:

'''
    使用aiohttp搭建web服务
'''
import asyncio
from aiohttp import web

async def index(request):
    await asyncio.sleep(0.5)
    return web.Response(body='<h1>Index</h1>')


async  def hello(request):
    await asyncio.sleep(0.5)
    text = '<h1>hello, %s</h1>' % request.match_info['name']
    return web.Response(body=text.encode('utf-8'),content_type='text/html')

async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('GET','/',index)
    app.router.add_route('GET','/hello/{name}',hello)
    srv = await loop.create_server(app.make_handler(),'0.0.0.0',6000)
    print('Server started at http://0.0.0.0:6000')
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

  This time, you can access the web service through the address

 

Note that aiohttpthe initialization function init()is a coroutine, loop.create_server()then use asyncioto create TCP services.

  

 

Guess you like

Origin www.cnblogs.com/xingxia/p/python_aiohttp.html