Python并发编程有多种方式,其中concurrent.futures
和asyncio
是比较流行的两种方式。本文将介绍如何使用concurrent.futures
和asyncio
实现并发编程,包括如何使用线程池和进程池,以及如何使用异步编程和协程。
concurrent.futures实例
线程池示例
线程池的使用可以大大减少线程的创建和销毁,提高程序的性能。下面是一个使用线程池的示例,该示例调用了concurrent.futures.ThreadPoolExecutor
来创建一个线程池,然后使用submit
方法往线程池中提交任务,最后通过as_completed
方法获取执行结果。
import concurrent.futures
import time
def worker(num):
"""
模拟耗时工作
"""
print(f"start working {num}")
time.sleep(1)
print(f"done working {num}")
return num * num
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
# 提交任务
results = [executor.submit(worker, i) for i in range(5)]
# 获取执行结果
for future in concurrent.futures.as_completed(results):
result = future.result()
print(f"result: {result}")
输出:
start working 0
start working 1
done working 0
result: 0
start working 2
done working 1
result: 1
start working 3
done working 2
result: 4
start working 4
done working 3
result: 9
done working 4
result: 16
进程池示例
进程池的使用与线程池类似,只需要将ThreadPoolExecutor
改为ProcessPoolExecutor
即可。下面是一个使用进程池的示例,该示例调用了concurrent.futures.ProcessPoolExecutor
来创建一个进程池,然后使用submit
方法往进程池中提交任务,最后通过as_completed
方法获取执行结果。
import concurrent.futures
import time
def worker(num):
"""
模拟耗时工作
"""
print(f"start working {num}")
time.sleep(1)
print(f"done working {num}")
return num * num
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
# 提交任务
results = [executor.submit(worker, i) for i in range(5)]
# 获取执行结果
for future in concurrent.futures.as_completed(results):
result = future.result()
print(f"result: {result}")
输出:
start working 0
start working 1
done working 0
result: 0
start working 2
start working 3
done working 1
result: 1
done working 2
result: 4
start working 4
done working 3
result: 9
done working 4
result: 16
asyncio实例
异步编程示例
使用asyncio
实现异步编程,需要用到async
和await
关键字。下面是一个使用asyncio
实现异步编程的示例,该示例定义了一个协程函数count
,使用await asyncio.sleep(1)
模拟耗时操作,然后通过await asyncio.gather(*tasks)
等待多个协程执行完成。
import asyncio
import time
async def count(num):
"""
模拟耗时操作
"""
print(f"start working {num}")
await asyncio.sleep(1)
print(f"done working {num}")
return num * num
async def main():
"""
异步执行任务
"""
tasks = [count(i) for i in range(5)]
results = await asyncio.gather(*tasks)
for result in results:
print(f"result: {result}")
asyncio.run(main())
输出:
start working 0
start working 1
done working 0
result: 0
start working 2
done working 1
result: 1
start working 3
done working 2
result: 4
start working 4
done working 3
result: 9
done working 4
result: 16
协程示例
协程是一个轻量级的并发编程方式,可以用async
和await
关键字定义。下面是一个使用协程实现异步编程的示例,该示例定义了一个协程函数count
,使用await asyncio.sleep(1)
模拟耗时操作,然后使用asyncio.create_task
方法创建一个任务,并通过await task
等待任务执行完成。
import asyncio
import time
async def count(num):
"""
模拟耗时操作
"""
print(f"start working {num}")
await asyncio.sleep(1)
print(f"done working {num}")
return num * num
async def main():
"""
协程执行任务
"""
tasks = [count(i) for i in range(5)]
for task in tasks:
result = await task
print(f"result: {result}")
asyncio.run(main())
输出:
start working 0
done working 0
result: 0
start working 1
done working 1
result: 1
start working 2
done working 2
result: 4
start working 3
done working 3
result: 9
start working 4
done working 4
result: 16
以上是Python并发编程中concurrent.futures
和asyncio
的使用示例,可以根据具体场景选择不同的方式来实现并发编程。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python并发concurrent.futures和asyncio实例 - Python技术站