In Python, coroutine is a lightweight concurrent programming method that can achieve efficient concurrent execution through collaborative multitasking. Using the yield keyword to suspend the execution of a function and save the current execution status is a special feature of coroutines. Therefore, a coroutine can be considered a special kind of generator function. When a coroutine is suspended, its execution can be resumed using the send method, and a value is returned upon resumption.
Before Python 3.4, the yield keyword was often used to implement coroutines, which was called "generator coroutines". After the asyncio module was introduced in Python 3.4, you can use the async/await keyword to define coroutine functions, called "native coroutines".
Compared with threads and processes, coroutines have the following advantages:
Lightweight: The context switching cost of coroutines is very small and can be concurrent within a single thread. Execute a large number of coroutines.
Low latency: During the execution of the coroutine, there is no overhead of thread switching or locking and unlocking, allowing faster response to external events.
Efficiency: Coroutine code is usually more concise and readable than multi-thread and multi-process code, and has lower maintenance costs.
The usage scenarios of coroutines include network programming, asynchronous I/O, data stream processing, high-concurrency tasks, etc.
In Python 3, Generator Coroutine refers to a coroutine implemented using generator functions. A generator function is a special function that returns a generator object. The execution of the function can be paused through the yield statement, and then continues the next time the generator object's"next"() method is called.
The following is an example of a simple generator coroutine, which contains a generator function coroutine and a simple asynchronous I/O operation:
import asyncio def coroutine(): print('Coroutine started') while True: result = yield print('Coroutine received:', result) async def main(): print('Main started') c = coroutine() next(c) c.send('Hello') await asyncio.sleep(1) c.send('World') print('Main finished') asyncio.run(main())
Result output:
[root@workhost k8s]# python3 test.py
Main started
Coroutine started
Coroutine received: Hello
Coroutine received: World
Main finished
Let’s take a look at the execution process of the above code:
The main function starts to execute and prints out Main started.
Create a generator object c and call next(c) to pause its execution at the first yield statement.
Use c.send('Hello') to resume the execution of the generator function and use 'Hello' as the return value of the generator function.
While waiting for 1 second, the main function pauses execution and waits for the event loop to initiate the next task.
After waiting for 1 second, use c.send('World') to continue executing the generator function and use 'World' as the return value of the generator function.
The main function resumes execution and prints out Main finished.
By using the generator function coroutine, this code implements a simple coroutine. A generator function pauses the execution of the function by using the yield statement, which can then be resumed via the send method, passing the value to the generator function. In this way, you can use generator functions to achieve asynchronous concurrency. Use a generator function to accept the results of an asynchronous I/O operation and print them out, as shown in the example.
Python 3 introduces Native Coroutine as a new coroutine type. Native coroutines are defined by using the async/await keyword, and unlike generator coroutines, they can return values like normal functions using the return statement instead of using the yield statement.
The following is a simple native coroutine example, which contains a coroutine function modified with the async keyword and a simple asynchronous I/O operation:
import asyncio async def coroutine(): print('Coroutine started') await asyncio.sleep(1) print('Coroutine finished') async def main(): print('Main started') await coroutine() print('Main finished') asyncio.run(main())
Result output:
[root@workhost k8s]# python3 test.py
Main started
Coroutine started
Coroutine finished
Main finished
Continue watching Here is the execution process:
The main function starts executing and Main started is printed.
Call the coroutine function and run it as a coroutine object.
In the coroutine function, print out Coroutine started.
In the coroutine function, use await asyncio.sleep(1) to pause the execution of the function and wait for 1 second.
After 1 second, resume the execution of the coroutine function and print out Coroutine finished.
The main function resumes execution and prints out Main finished.
In the above code, a native coroutine function coroutine is defined using the async keyword, and the await keyword is used in it to pause the execution of the function and wait for asynchronous I/O operations. of completion. Using native coroutines allows you to write concurrent asynchronous code, thereby improving code efficiency and performance.
Python 3 中,原生协程和生成器协程是不同的协程实现方式,它们分别具有独特的特点和适用场景。下面,通过对比它们的区别和优缺点,才可以更好地理解它们之间的异同,以便选择适合自己的协程实现方式,从而更好地编写高效、可维护的异步程序。
1.区别:
定义方式不同:原生协程使用 async/await 关键字来定义,而生成器协程使用 yield 关键字来定义。
返回方式不同:原生协程使用 return 语句来返回结果,而生成器协程使用 yield 语句来返回结果。
调用方式不同:原生协程使用 await 关键字来调用,而生成器协程使用 yield from 或 yield 语句来调用。
原生协程与生成器协程的实现方式不同,前者使用 asyncio 库,后者则是 Python 语言内置的特性。
2.优缺点:
原生协程的优点:
代码简洁易懂:使用 async/await 关键字,可以编写出更简洁易懂的协程代码。
性能更高:原生协程不需要创建生成器对象,也不需要通过 yield 语句来控制函数的执行流程,因此能够更加高效地处理异步操作。
支持异步 I/O 和任务处理:原生协程可以支持异步 I/O 操作和并发任务处理,可以在处理异步操作时更加灵活。
原生协程的缺点:
兼容性差:原生协程是 Python 3.5 版本之后才引入的新特性,因此在旧版本的 Python 中无法使用。
异常处理不方便:原生协程在处理异常时比较麻烦,需要使用 try/except 语句来处理。
生成器协程的优点:
兼容性好:生成器协程是 Python 2 和 Python 3 都支持的特性。
可读性好:生成器协程使用 yield 关键字来实现,代码逻辑清晰易懂。
异常处理方便:生成器协程在处理异常时比较方便,可以使用 try/except 语句来处理。
生成器协程的缺点:
性能相对较低:生成器协程需要创建生成器对象,也需要通过 yield 语句来控制函数的执行流程,因此处理异步操作时性能相对较低。
功能有限:生成器协程不能像原生协程一样支持异步 I/O 操作和任务处理。
接下来,模拟一个场景,假设实现一个异步的批量处理任务的工具,使用原生协程来实现。
看下面代码:
import asyncio import random async def batch_process_task(tasks, batch_size=10): # 将任务列表划分为多个批次 for i in range(0, len(tasks), batch_size): batch = tasks[i:i+batch_size] # 使用原生协程来异步处理每个批次的任务 await asyncio.gather(*[process_task(task) for task in batch]) async def process_task(task): # 模拟任务处理过程 await asyncio.sleep(random.uniform(0.5, 2.0)) print("Task {} processed".format(task)) async def main(): # 构造任务列表 tasks = [i for i in range(1, 101)] # 并发处理批量任务 await batch_process_task(tasks, batch_size=10) if __name__ == '__main__': asyncio.run(main())
输出:
[root@workhost k8s]# python3 test.py
Task 9 processed
Task 10 processed
Task 1 processed
Task 8 processed
Task 6 processed
Task 4 processed
Task 3 processed
Task 2 processed
Task 5 processed
...
...
batch_process_task函数使用原生协程来处理每个批次的任务,而process_task函数则是处理每个任务的函数。在main函数中,任务列表会被构造,并使用batch_process_task函数来异步地处理批量任务。
The above is the detailed content of What are the implementation methods of Python coroutines?. For more information, please follow other related articles on the PHP Chinese website!