Where the co-process comes in
Concurrency of large systems and easy to bottleneck in IO (disk IO, network IO), multi-threaded, multiple processes can solve this problem, of course, thread, process switching is very resource-intensive. The best solution is to use a single-threaded approach to solve the concurrency IO problem-This is where the coprocessor works.
The process is actually single-threaded in scheduling, is unable to take advantage of multi-core CPU, so for compute-intensive tasks still need to consider the way of multi-process + coprocessor.
Reference:
http://blog.csdn.net/qq910894904/article/details/41699541
Http://www.cnblogs.com/xone/p/6198500.html
The Asyncio of Asynchronous IO Library
The Asyncio is a standard library introduced in Python version 3.4, and is built directly with support for asynchronous IO.
The Asyncio programming model is a message loop. Asynchronous IO is implemented by taking a eventloop reference directly from the Asyncio module and then throwing the required execution into the EventLoop execution.
Import asyncioimport threading@asyncio.coroutinedef Hello (index): print ('Hello world! index=%s,thread=%s'%(Index,threading.currentthread ()))yield fromAsyncio.sleep (1) Print ('Hello again! index=%s,thread=%s'%(Index,threading.currentthread ())) Loop=Asyncio.get_event_loop () Tasks=[hello (i) forIinchRange10000)]loop.run_until_complete (asyncio.wait (Tasks)) Loop.close ()
import asyncio@asyncio.coroutinedef wget (URL): Print ('wget%s ...'%(URL)) connection= Asyncio.open_connection (URL, the) Reader,writer=yield fromConnection Header='get/http/1.0\r\nhost:%s\r\n\r\n'%URL writer.write (Header.encode ('Utf-8')) yield fromWriter.drain () whileTrue:line=yield fromReader.readline ()ifline = = B'\ r \ n': BreakPrint ('%s Header >%s'% (Url,line.decode ('Utf-8'). Rstrip ())) Writer.close () Loop=Asyncio.get_event_loop () Tasks=[wget (URL) forUrlinch['www.sina.com.cn','www.sohu.com','www.163.com']]loop.run_until_complete (asyncio.wait (Tasks)) Loop.close ()
Async and await
- To simplify and better identify asynchronous IO, new syntax async and await are introduced from Python 3.5 to make Coroutine's code more concise and readable. (In fact, net also has this stuff)
Reference:
Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 00144661533005329786387b5684be385062a121e834ac7000
Aiohttp
- Aiohttp is an HTTP framework based on the Asyncio implementation
- The HTTP connection is an IO operation, so high concurrency support for multiple users can be achieved with single-threaded +coroutine
Import Asyncio fromaiohttp Import WebAsyncdef index (request):awaitAsyncio.sleep (0.5) returnWeb. Response (body=b'')Asyncdef hello (request):awaitAsyncio.sleep (0.5) Text=''% request.match_info['name'] returnWeb. Response (Body=text.encode ('Utf-8'))Asyncdef init (loop): App= Web. Application (loop=loop) App.router.add_route ('GET','/', index) App.router.add_route ('GET','/hello/{name}', hello) SRV=awaitLoop.create_server (App.make_handler (),'127.0.0.1',8000) Print ('Server started at http://127.0.0.1:8000 ...') returnSrvloop=Asyncio.get_event_loop () loop.run_until_complete (init (loop)) Loop.run_forever ()
The report is abnormal:
From aiohttp Import web
Importerror:cannot import name ' web '
Does the above question not know because of the version issue? To be solved .....
Python's Asynchronous IO