Python 多线程和多进程并发执行
warning:
这篇文章距离上次修改已过181天,其中的内容可能已经有所变动。
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# 定义一个函数,模拟耗时任务
def long_running_task(task_name, delay):
print(f"任务 {task_name} 开始执行")
time.sleep(delay) # 模拟耗时操作
print(f"任务 {task_name} 执行完成")
return f"任务 {task_name} 的结果"
# 使用多线程执行任务
def run_with_threads(tasks):
with ThreadPoolExecutor(max_workers=4) as executor:
results = executor.map(long_running_task, tasks.keys(), tasks.values())
for result in results:
print(result)
# 使用多进程执行任务
def run_with_processes(tasks):
with ProcessPoolExecutor() as executor:
results = {executor.submit(long_running_task, task, delay): task for task, delay in tasks.items()}
for future in executor.as_completed(results):
task = results[future]
try:
data = future.result()
print(data)
except Exception as e:
print(f"任务 {task} 执行出错: {e}")
# 示例使用
tasks = {'task1': 2, 'task2': 4, 'task3': 6, 'task4': 8}
run_with_threads(tasks)
print("\n")
run_with_processes(tasks)
这段代码定义了一个模拟耗时任务的函数long_running_task
,以及两个函数run_with_threads
和run_with_processes
,分别用于使用多线程和多进程并发执行这些任务。run_with_threads
使用ThreadPoolExecutor
来执行任务,而run_with_processes
使用ProcessPoolExecutor
。两个函数都通过迭代传入的任务字典来启动并执行任务,并打印出任务结果或错误信息。
评论已关闭