- After python3.4 the asyncio new module, it can help us detect IO (IO [HTTP only network connection is network IO operations]), implement application-level switch ( asynchronous IO ). Note: asyncio tcp level can only send a request can not send http protocol.
- Asynchronous IO: the so-called "asynchronous IO," is that you initiate a network IO operation, but do not wait for it to end, you can continue to do other things, when it ends, you will be notified.
- implementation: + Coroutine threaded asynchronous IO operations.
- Asynchronous coroutine usage
Next, let's look at coroutine achieved, starting from Python 3.4, Python added the concept of coroutines, but this version of the coroutine or to produce object-based, in Python 3.5 increase the async / await , so as to achieve more convenient coroutine. First, we need to understand the following concepts:
-
event_loop: event loop, the equivalent of an infinite loop, we can register some functions to this event loop when the condition occurs, it will call the corresponding treatment.
-
coroutine: Chinese translation is called coroutines, on behalf of the Association refers to the process object types in Python often, we can register coroutine object to the cycle time, it will be calling the cycle events. We can use async keyword to define a method that will not be executed immediately when you call, but returns a coroutine object.
-
task: task, which is further encapsulated to coroutine object contains the status of each task.
-
future: a guide to future results or perform tasks not performed, and the task is actually no essential difference.
In addition, we also need to know async / await keywords, it only appeared from Python 3.5, especially for defining the coroutine. Wherein, async define a coroutine, await a method for blocking pending execution.
- definition of a coroutine
Example:
from Time Import SLEEP Import ASYNCIO the async DEF request (URL): Print ( ' being requested URL ' ) SLEEP ( 2 ) Print ( ' download successful ' ) # returns a special coroutine object request function is not executed inside c = Request ( ' www.baidu.com ' ) # instantiate an object is the event loop loop = asyncio.get_event_loop () # event loop object creates a task object, and coroutine encapsulated into objects of the object based on the task = loop.create_task ( C) # another form of the method of task objects instantiated task =asyncio.ensure_future (c) # The coroutine object is registered to the event loop object, and the object need to start the event loop # as the first argument in the event loop objects encountered obstruction is behind the object automatically executed when the first the end of an object is blocking event loop will be reported to the object, and then continue with the event loop objects first object, so as to achieve the effect of asynchronous loop.run_until_complete (task)
2. callback object bound to the task
Import ASYNCIO the async DEF Request (URL): Print ( ' being requested URL ' ) Print ( ' download successful ' ) return URL # callback function must have a parameter: task task object [] # task.result (): task object encapsulation special coroutine inside the object corresponding to the function return value DEF the callback (task): Print ( ' the this the callback iS ' ) Print (task.result ()) C = Request ( ' www.baidu.com ' ) # Create a task objects task =asyncio.ensure_future (c) # to the task object to bind a callback function task.add_done_callback (callback) # instantiate an event loop target Loop = asyncio.get_event_loop () # will co-drive the object into the event loop object and need to start event loop objects loop.run_until_complete (task)
3. Multi-task asynchronous coroutine
Import ASYNCIO Import Time URLs = [ ' www.baidu.com ' , ' www.sogou, COM ' , ' www.goubanjia.com ' ] START_TIME = the time.time () the async DEF Request (URL): Print ( ' requesting url ' ) # multitasking asynchronous coroutine matters, can not appear relevant code does not support asynchronous, sleep does not support # SLEEP (2) the await asyncio.sleep (2 ) Print ( ' download successful ' ) Loop =asyncio.get_event_loop () # task list: a plurality of task objects to prevent Tasks = [] for URL in URLs: C = Request (URL) Task = asyncio.ensure_future (C) tasks.append (Task) loop.run_until_complete (asyncio.wait (Tasks)) Print (time.time () - start_time)
4. Application of Multi asynchronous task coroutine
# Aiohttp: a support module of the network-based asynchronous request Import aiohttp Import ASYNCIO Import Time URLs = [ ' http://127.0.0.1:5000/jay ' , ' http://127.0.0.1:5000/bobo ' , ' http://127.0.0.1:5000/tom ' ,] START_TIME = the time.time () the async DEF get_pageText (URL): the async with aiohttp.ClientSession () AS S: # instance of the requested object async with await s.get ( URL) AS Response: page_text = the await response.text () Print (page_text) # where the return value, because the callback function to use for data analysis return page_text # package callback function for data analysis DEF the parse (Task): # 1. acquiring data corresponding page_text = task.reault () Print ( + page_text ' , upcoming data analysis ... ' ) # the following parsing operations tasks = [] for URL in URLs: C = get_pageText (URL) task = asyncio.ensure_future (C) # to the callback function for the task object binding data analytical task.add_done_callback (parse) tasks.append (Task) Loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) print(time.time() - start_time)