Asyncio
This module is a new feature added to version 3.4.
Let's look at an example:
Def a (): for x in range (3): print (' a.x ', x) def b (): For x in ' abc ': print (' b.x ', X) A () b () #运行结果: a.x 0a.x 1a . x 2b.x ab.x bb.x C
This example is a typical serial program, and two function calls are executed sequentially in the main thread.
There are several ways to change the program to parallel:
1. Generator
2. Multithreading
3. Multi-process
4. Co-process
1) Generator Method:
Def a (): for x in range (3): yield xdef B (): For x in ' abc ': yield XM = A () n = B () for _ in range (3): PR Int (next (m)) print (Next (n)) #运行结果: 0A1B2C
Use the generator to implement alternating execution. Both of these functions have the opportunity to execute, such that the schedule is not the process of the operating system, the thread completes, but the user designed it.
2) Multithreading Method:
Import Threading,timedef A (): for x in range (3): time.sleep (0.0001) print (' a.x ', x) def b (): For x in ' ABC ': time.sleep (0.0001) print (' b.x ', x) threading. Thread (Target=a). Start () threading. Thread (target=b). Start () #运行结果: a.x 0b.x aa.x 1b.x ba.x 2b.x C
The main use of the sleep function to force the switch to implement pseudo-parallelism.
3) Multi-process mode:
Import Multiprocessingdef A (): for x in range (3): print (' a.x ', x) def b (): For x in ' abc ': print (' b.x ', x) if __name__ = = ' __main__ ': multiprocessing. Process (Target=a). Start () multiprocessing. Process (target=b). Start () #运行结果: a.x 0a.x 1a.x 2b.x ab.x bb.x C
The multi-process approach is really parallel.
4) Co-process method:
The Asyncio standard library, which is a new feature added to the Python3.4 version, is based on selectors implementations, including asynchronous Io, event loops, and co-processes.
Event Loop:
The event loop is the core operating mechanism provided by Asyncio.
The program opens an infinite loop, and the user registers some functions with the event loop. When the event occurs, the corresponding function is called.
4.1 Event Loop base class
Asyncio. Baseeventloop This class is an implementation detail and it is Asyncio. Abstracteventloop sub-class, can not be used directly
Asyncio. The abstract base class for the Abstracteventloop event loop, which is thread insecure
4.2 Running the event loop
Asyncio.get_event_loop () Returns an event loop object, which is Asyncio. Examples of Baseeventloop
Asyncio. Abstracteventloop.stop () stop running the event loop
Asyncio. Abstracteventloop.run_forever () runs until the Stop () is called
Asyncio. Abstracteventloop.run_until_complete (future) runs until the future object finishes running, returning the result
Asyncio. Abstracteventloop.close () Close event loop
Asyncio. Abstracteventloop.is_running () returns the run state of the event loop
Asyncio. Abstracteventloop.is_closed () returns True if the event loop is closed
4.3 Co-process
The association is not a process, nor a thread, it is the way that user-space scheduling completes concurrent processing. (Alternating execution in the same thread is actually pseudo concurrency)
Concurrency refers to how much is done over a period of time, and parallel to how many simultaneous executions are performed at the same time.
Processes, threads are dispatched by the operating system, and the threads are scheduled to complete within the thread. It does not require more threads, and there is no overhead associated with multi-threaded switching.
The co-process is non-preemptive, and only one of the processes actively yields control, and the other process is dispatched.
The process also does not require a lock mechanism because it is executed in the same thread.
Multi-CPU, you can use multi-threaded and co-ordination, both process concurrency and can play the advantages of the single-threaded path.
In Python, the coprocessor is based on the generator.
4.4 Use of the co-process
4.4.1 Python3.4 using @asyncio.coroutine, yield from
#asyncio python3.4import asyncio@asyncio.coroutinedef foo (x): #生成器函数上面加了协程装饰器之后就转化成协程函数for I in range (3):p rint (' foo {} '. Format (i)) yield from asyncio.sleep (x) #调用另一个生成器对象loop = Asyncio.get_event_loop () #获得一个时间循环loop. Run_until_complete ( Foo (1)) #传入一个生成器对象的调用loop. Close () #运行结果: foo 0foo 1foo 2[finished in 3.3s]
This example converts the generator function into a co-function after a generator function has been added to the co-designer.
4.4.2 Python3.5 uses the keyword async def, await, and natively supports syntax
#asyncio python3.5import asyncioasync def foo (x): #异步定义, the association defines the for I in Range (3):p rint (' foo {} '. Format (i)) await Asyncio.sleep (x) #不可以出现yield, use await to replace print (Asyncio.iscoroutinefunction (foo)) loop = Asyncio.get_event_loop () Loop.run_until_complete (foo (1)) #传入一个协程对象的调用loop. Close () #运行结果: Truefoo 0foo 1foo 2[finished in 3.3s]
The async def is used to define the Iscoroutinefunction function, and Func determines whether the Func function is a co-function. The await, async keyword may not be included in the co-function, but the yield keyword cannot be used.
Other syntax: Async with, support for context-enabled processes
4.4.3 Coroutine asyncio.wait (Futures, *, Loop=none, Timeout=none, return_when=all_completed)
The futures sequence cannot be empty while waiting for the futures in the sequence to finish executing.
Timeout can be used to control the maximum number of seconds to wait before returning, the number of seconds can be an int or a floating point, or no limit if timeout is not specified.
#wait多个协程对象import Asyncio@asyncio.coroutinedef A (): For I in range (3):p rint (' a.x ', i) Yield@asyncio.coroutinedef B (): For I in range (3):p rint (' b.x ', i) Yieldloop = Asyncio.get_event_loop () tasks = [A (), B ()]loop.run_until_complete ( Asyncio.wait (Tasks)) #传入一个协程对象序列loop. Close () #运行结果: b.x 0a.x 0b.x 1a.x 1b.x 2a.x 2[finished in 0.3s]
Summarize:
Traditional multi-threaded, multi-process is the system to complete the scheduling, and the process is in the thread of the processes by the user-space scheduling to complete the concurrent processing, mainly rely on the generator to achieve alternate scheduling.
Python3.4 using @asyncio.coroutine, yield from to invoke another generator object
The keyword Async def and await are used in Python3.5, and the yield keyword cannot be present.
[Python Multithreading] Asyncio (16)