10个python线程池|进程池|小案例
作者:YXN-python 阅读量:66 发布日期:2024-05-21
线程池
1、执行并发任务
import concurrent.futures
def task(name):
print(f"Task {name} started")
# 执行任务的代码
print(f"Task {name} completed")
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(task, range(10))
2、进行并发计算
from concurrent.futures import ThreadPoolExecutor, as_completed
# 要在线程中运行的函数
def worker(n):
return n * n
# 主程序
def main():
# 创建一个线程池,最大线程数为4
with ThreadPoolExecutor(max_workers=4) as executor:
# 提交任务到线程池中
future_to_result = {executor.submit(worker, n): n for n in range(10)}
# 获取结果
for future in as_completed(future_to_result):
n = future_to_result[future]
try:
data = future.result()
print(f'{n} 平方: {data}')
except Exception as e:
print(f'{n} 异常: {e}')
if __name__ == '__main__':
main()
3、下载多个文件
import concurrent.futures
import requests
def download_file(url):
response = requests.get(url)
# 将文件保存到本地
filename = url.split("/")[-1]
with open(filename, "wb") as file:
file.write(response.content)
print(f"Downloaded {url}")
urls = [
"https://example.com/file1.txt",
"https://example.com/file2.txt",
"https://example.com/file3.txt",
]
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(download_file, urls)
4、惰性读取大文件
import concurrent.futures
def process_line(line):
# 处理每行数据的代码
pass
with open("large_file.txt", "r") as file:
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(process_line, file)
5、惰性筛选质数
import concurrent.futures
def is_prime(number):
# 判断一个数是否为质数的代码
pass
def generate_numbers():
number = 2
while True:
yield number
number += 1
with concurrent.futures.ThreadPoolExecutor() as executor:
numbers = generate_numbers()
primes = filter(is_prime, numbers)
for prime in primes:
print(prime)
进程池
1、执行并发任务
import concurrent.futures
def task(name):
print(f"Task {name} started")
# 执行任务的代码
print(f"Task {name} completed")
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(task, range(10))
2、压缩多个文件
import concurrent.futures
import shutil
def compress_file(filename):
compressed_filename = f"{filename}.zip"
shutil.make_archive(compressed_filename, "zip", filename)
print(f"Compressed {filename}")
files = [
"file1.txt",
"file2.txt",
"file3.txt",
]
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(compress_file, files)
3、处理大规模数据
import concurrent.futures
def process_data(data):
# 处理数据的代码
return processed_data
data = [...] # 大规模数据
with concurrent.futures.ProcessPoolExecutor() as executor:
results = executor.map(process_data, data)
for result in results:
# 处理处理后的数据
pass
4、计算无限序列的平方数
import concurrent.futures
def calculate_square(number):
return number ** 2
with concurrent.futures.ProcessPoolExecutor() as executor:
numbers = range(1, 1000000)
results = executor.map(calculate_square, numbers)
for result in results:
print(result)
5、进行惰性计算斐波那契数列的第n项
import concurrent.futures
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
with concurrent.futures.ProcessPoolExecutor() as executor:
n = 10
result = executor.submit(fibonacci, n)
print(result.result())
YXN-python
2024-05-21