一. 前言

协程(Coroutine)是一种轻量级的线程,也被称为用户级线程或绿色线程。它是一种用户态的上下文切换方式,比内核态的线程切换更为轻量级,能够高效的支持大量并发操作。

2. 使用协程的好处

Python 中的协程是通过 asyncio 模块实现的,使用协程可以带来以下好处:

  1. 轻量级并发操作:协程的运行过程中不需要进行线程切换和上下文切换,避免了这些操作的开销,因此比传统的线程更加轻量级,可以支持更多并发操作。
  2. 提高程序性能:协程避免了线程上下文切换的开销,可以大大提高程序的执行效率。并且在 Python 中,协程的调度是由事件循环(Event
    Loop)来完成的,事件循环采用的是单线程方式,能够充分利用 CPU 资源,进一步提高了程序的性能。
  3. 简化异步编程:协程使得异步编程更加简单,在编写异步程序时可以避免回调地狱(Callback
    Hell)的问题,提高程序的可读性和可维护性。而且通过 Python 的 async/await
    语法,可以使用同步的方式编写异步代码,使得代码更加易于理解和调试。
  4. 更加灵活的控制流程:协程可以方便地进行挂起和恢复操作,因此可以很灵活地控制流程。例如可以在协程中使用条件判断、循环等语句,进行更加复杂的流程控制操作。

总之,协程是一种高效、轻量级的并发编程方式,能够提高程序的性能,简化异步编程,使得控制流程更加灵活,是 Python 中重要的并发编程工具之一。

三. 代码示例

1. 基本使用

import asyncio

async def task1():
    await asyncio.sleep(1)
    print('Task 1 done')

async def task2():
    await asyncio.sleep(2)
    print('Task 2 done')

async def main():
    print('Starting tasks')
    # 并发执行 task1 和 task2 任务
    await asyncio.gather(task1(), task2())
    print('All tasks done')

asyncio.run(main())

运行结果
Python - 协程基本使用详解【demo】-LMLPHP

2. 进阶使用:异步任务循环任务

方法一:

import asyncio
import random

async def producer(queue):
    while True:
        value = random.randint(0, 10)
        print(f"Produced: {value}")
        await queue.put(value)
        await asyncio.sleep(random.random())

async def consumer(queue):
    while True:
        value = await queue.get()
        print(f"Consumed: {value}")
        await asyncio.sleep(random.random())

async def main():
    queue = asyncio.Queue()
    task_producer = asyncio.create_task(producer(queue))
    task_consumer = asyncio.create_task(consumer(queue))
    await asyncio.gather(task_producer, task_consumer)

asyncio.run(main())

在上面的代码中,我们定义了一个 producer 函数和一个 consumer 函数,它们都接受一个 asyncio.Queue 对象作为输入,并在其中实现协程的逻辑。其中 producer 函数会在队列中不断生成随机数,并将其放入队列中;consumer 函数则会不断从队列中取出随机数并进行消费。

在 main 函数中,我们首先创建了一个 asyncio.Queue 对象用于协程之间的通信,然后使用 asyncio.create_task 函数创建了两个任务,分别是生产者任务和消费者任务。最后,我们使用 asyncio.gather 函数来运行这两个任务,当其中任何一个任务完成时,main 函数也会结束。

需要注意的是,当我们使用协程时,需要使用 await 关键字来挂起协程的执行,等待其他协程的执行或者等待 I/O 操作完成。同时,在协程中不应该使用阻塞式的操作,比如 time.sleep(),而应该使用异步 I/O 操作,比如 asyncio.sleep()

方法二

import asyncio
import random


async def producer(queue):
    while True:
        value = random.randint(0, 10)
        print(f"Produced: {value}")
        await queue.put(value)
        await asyncio.sleep(random.random())

async def consumer(queue):
    while True:
        value = await queue.get()
        print(f"Consumed: {value}")
        await asyncio.sleep(random.random())


async def main():
    queue = asyncio.Queue()
    '''方法一'''
    # task_producer = asyncio.create_task(producer(queue))
    # task_consumer = asyncio.create_task(consumer(queue))
    # await asyncio.gather(task_producer, task_consumer)
    '''方法二'''
    await asyncio.wait([producer(queue), consumer(queue)])


asyncio.run(main())

以上协程的两种启动方式的的区别在于它们等待任务完成的方式不同。

await asyncio.gather(task_producer, task_consumer) 会并发地运行多个任务,并等待它们全部完成后才会返回结果。也就是说,这种方式只有在所有任务都执行完成后,才会进入下一步。

await asyncio.wait([producer(queue), consumer(queue)]) 则是将多个协程对象(通过列表进行传递)传递给 asyncio.wait() 函数,这个函数也会等待多个协程同时完成。不同的是,asyncio.wait() 函数会返回两个集合(即 done 和 pending),done 集合包含已经完成的任务,pending 集合包含还未完成的任务。因此可以通过遍历 done 集合取得协程的结果。

总的来说,asyncio.gather() 更加简单易用,适合并发执行多个任务并等待它们全部完成的情况。而 asyncio.wait() 则更加灵活,并且可以实时获取任务的执行结果。

3. 启动方式差异

注意,在 Python 3.7 以前,我们需要使用 asyncio.get_event_loop().run_until_complete() 函数来运行协程任务。但在 Python 3.7 及以后的版本中,我们可以使用更为简洁的 asyncio.run() 函数来运行协程任务。
示例代码如下:

import asyncio
import random


async def producer(queue):
    while True:
        value = random.randint(0, 10)
        print(f"Produced: {value}")
        await queue.put(value)
        await asyncio.sleep(random.random())

async def consumer(queue):
    while True:
        value = await queue.get()
        print(f"Consumed: {value}")
        await asyncio.sleep(random.random())


async def main():
    queue = asyncio.Queue()
    '''方法一'''
    # task_producer = asyncio.create_task(producer(queue))
    # task_consumer = asyncio.create_task(consumer(queue))
    # await asyncio.gather(task_producer, task_consumer)
    '''方法二'''
    await asyncio.wait([producer(queue), consumer(queue)])


'''协程启动的方式一'''
# asyncio.run(main())

'''协程启动的方式二'''
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
new_loop.run_until_complete(main())

总之,协程是一种高效、轻量级的并发编程方式,能够提高程序的性能,简化异步编程,使得控制流程更加灵活,是 Python 中重要的并发编程工具之一。

以上就是关于Python - 协程基本使用的介绍,希望对你有所帮助,谢谢!

08-18 20:01