基于Python的服务器设计与异步编程实践
1. 网络编程基础回顾
在网络编程中,Python的socket模块是实现客户端 - 服务器架构的重要工具,可配合TCP和UDP协议使用。同时,结合SSL模块,还能进行基本的TCP/IP套接字编程。以下是一些关于socket编程的关键知识点:
-
TCP和UDP协议实现
:使用socket模块可以在Python中实现TCP和UDP协议。例如,简单的TCP套接字可以通过TLS进行封装,用于传输加密数据。
-
服务器真实性验证
:借助SSL证书可以验证远程服务器的真实性。
-
其他问题
:还涉及到非阻塞套接字I/O等一些套接字编程的小问题。通过Wireshark进行详细的数据包分析,有助于理解套接字编程脚本的底层原理。
1.1 常见问题解答
| 问题 | 解答 |
|---|---|
| 哪个socket模块的方法允许服务器套接字接受来自其他主机的客户端套接字请求? | 文中未明确提及具体方法。 |
| 哪个socket模块的方法允许向给定地址发送数据? | 文中未明确提及具体方法。 |
| 哪个socket模块的方法允许将主机和端口与特定套接字关联? | 文中未明确提及具体方法。 |
| TCP和UDP协议的区别是什么,如何在Python中使用socket模块实现它们? | TCP是面向连接的、可靠的协议,UDP是无连接的、不可靠的协议。使用socket模块创建套接字时,通过指定不同的参数可以分别实现TCP和UDP协议。 |
| 哪个socket模块的方法允许使用套接字实现端口扫描并检查端口状态? | 文中未明确提及具体方法。 |
| Windows系统上用于在环回接口捕获数据包的替代工具是什么? | 文中未提及。 |
| 客户端 - 服务器IPv6协议的套接字配置是什么? | 文中未提及。 |
| 从Python 3.4+版本开始,我们可以使用哪个模块提供的API快速构建基于I/O原语的面向对象服务器? | 文中未提及。 |
| 可以使用SSL模块的哪些方法和参数来建立SSL套接字连接? | 文中未提及。 |
| 可以使用SSL模块的哪个方法提取远程主机证书详细信息并验证远程服务器的真实性? | 文中未提及。 |
1.2 进一步学习资源
- Wireshark文档:https://wiki.wireshark.org
- Python 3中的套接字:https://docs.python.org/3/library/socket.html
- Python中的套接字编程:https://www.geeksforgeeks.org/socket-programming-python/
- https://realpython.com/python-sockets/
- Python 3.7套接字的新特性:https://www.agnosticdev.com/blog-entry/python/whats-new-sockets-python-37
2. 基于多进程的TCP服务器构建
2.1 concurrent.futures模块介绍
在Python 3中进行多进程编程时,有多种选择,其中
concurrent.futures
和
multiprocessing
模块较为突出。
concurrent.futures
模块是Python标准库的一部分,它为线程和多进程模块提供了一层简化的抽象层,将线程建模为异步任务。
futures
在处理异步任务时,与
promises
、
delay
或
deferred
同义,可看作是一个待处理的结果,通常表示计算尚未结束或网络传输尚未完成的结果。该模块有一个抽象基类
executor
,其两个子类分别为
ThreadPoolExecutor
(用于多线程)和
ProcessPoolExecutor
(用于多进程),通过
max_workers
参数可以指定异步执行调用的最大工作线程数。
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# 线程池执行器
executor_thread = ThreadPoolExecutor(max_workers=4)
# 进程池执行器
executor_process = ProcessPoolExecutor(max_workers=4)
2.2 异步文件下载示例
以下是一个使用
ThreadPoolExecutor
异步下载文件的示例代码:
#!/usr/bin/python3
from concurrent.futures import ThreadPoolExecutor
import requests
import itertools
import time
docs = [
'https://docs.python.org/3/archives/python-3.7.2-docs-pdf-letter.zip',
'https://docs.python.org/3/archives/python-3.7.2-docs-pdf-a4.zip',
'https://docs.python.org/3/archives/python-3.7.2-docs-html.zip',
'https://docs.python.org/3/archives/python-3.7.2-docs-text.zip',
'https://docs.python.org/3/archives/python-3.7.2-docs.epub'
]
def download_documents(documents, workers=4):
def get_document(url):
response = requests.get(url)
filename = url.split("/")[5]
print('Downloading '+ filename)
open(filename, 'wb').write(response.content)
return url
message = 'Downloading docs from https://docs.python.org/3/archives'
symbol = itertools.cycle('\|/-')
executor = ThreadPoolExecutor(max_workers=workers)
mydocs = [executor.submit(get_document, url) for url in documents]
while not all([doc.done() for doc in mydocs]):
print(message + next(symbol), end='\r')
time.sleep(0.1)
return mydocs
if __name__ == '__main__':
t1 = time.time()
print(download_documents(docs, workers=4))
print(time.time() - t1, 'seconds passed')
代码分析
-
get_document函数 :负责向指定URL发送请求并将文件下载到本地文件系统。 -
ThreadPoolExecutor实例化 :创建一个线程池执行器,将任务提交到线程池中执行。 -
Future对象 :mydocs列表中的每个元素都是一个Future对象,封装了可调用对象的异步执行。 -
Future.done()方法 :用于检查每个下载任务是否完成。
2.3 网站检查应用
以下是一个使用
concurrent.futures
包并发检查网站运行状态的示例代码:
#!/usr/bin/python3
import concurrent.futures
import requests
URLS = [
'http://www.foxnews.com/',
'http://www.cnn.com/',
'http://www.bbc.co.uk/',
'http://some-made-up-domain.com/'
]
# Retrieve a single page with requests module
def load_requests(domain):
with requests.get(domain, timeout=60) as connection:
return connection.text
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_executor = {executor.submit(load_requests, domain): domain for domain in URLS}
for domain_future in concurrent.futures.as_completed(future_executor):
domain = future_executor[domain_future]
try:
data = domain_future.result()
print('%r page is %d bytes' % (domain, len(data)))
except Exception as exception:
print('%r generated an exception: %s' % (domain, exception))
代码分析
-
load_requests函数 :尝试使用requests包与指定域名建立连接,并返回页面内容。 -
ThreadPoolExecutor:用于并发检查每个域名的状态。 -
concurrent.futures.as_completed方法 :返回一个迭代器,按完成顺序生成Future对象。
2.4 多进程方法
multiprocessing
模块是
threading
模块的替代方案,它与
threading
模块接口相似,但在底层使用进程而非线程。以下是一个使用
multiprocessing
模块检查网站状态的示例代码:
#!/usr/bin/python3
import time
import multiprocessing
import requests
from utils import check_website
from utils import WEBSITE_LIST
NUM_WORKERS = 3
if __name__ == '__main__':
start_time = time.time()
with multiprocessing.Pool(processes=NUM_WORKERS) as pool:
results = pool.map_async(check_website, WEBSITE_LIST)
results.wait()
print(results)
end_time = time.time()
print("Time for multiprocessing: %s secs" % (end_time - start_time))
utils.py
文件内容如下:
#!/usr/bin/python3
import requests
WEBSITE_LIST = [
'http://www.foxnews.com/',
'http://www.cnn.com/',
'http://www.bbc.co.uk/',
'http://some-other-domain.com/'
]
class WebsiteException(Exception):
pass
def ping_website(address, timeout=6000):
try:
response = requests.get(address)
print("Website %s returned status_code=%s" % (address, response.status_code))
if response.status_code >= 400:
print("Website %s returned status_code=%s" % (address, response.status_code))
raise WebsiteException()
except requests.exceptions.RequestException:
print("Timeout expired for website %s" % address)
raise WebsiteException()
def check_website(address):
try:
ping_website(address)
except WebsiteException:
pass
代码分析
-
ping_website函数 :向指定地址发送请求,检查响应状态码。如果状态码在400 - 500范围内,抛出WebsiteException异常。 -
check_website函数 :调用ping_website函数,并捕获WebsiteException异常。 -
multiprocessing.Pool:创建一个进程池,使用map_async方法并发处理WEBSITE_LIST中的每个地址。
2.5 多进程和线程池执行器对比
| 方法 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
concurrent.futures.ThreadPoolExecutor
| 线程开销小,适合I/O密集型任务 | 受GIL限制,不适合CPU密集型任务 | 网络请求、文件读写等I/O密集型操作 |
multiprocessing
| 可以充分利用多核CPU,适合CPU密集型任务 | 进程开销大,进程间通信复杂 | 数据处理、计算密集型任务 |
2.6 流程图
graph LR
A[开始] --> B[创建ThreadPoolExecutor或ProcessPoolExecutor]
B --> C[提交任务]
C --> D{任务是否完成}
D -- 是 --> E[获取结果]
D -- 否 --> C
E --> F[结束]
通过以上内容,我们学习了如何使用
concurrent.futures
和
multiprocessing
模块构建多进程的TCP服务器,以及如何并发处理任务。在实际应用中,需要根据任务的特点选择合适的方法。
3. 基于asyncio和aiohttp构建异步应用
3.1 asyncio模块介绍
asyncio
是Python标准库中的一个模块,从Python 3.4版本开始可用,其文档可在https://docs.python.org/3/library/asyncio.html 查看。它允许编写单线程的异步代码,实现Python中的并发编程。
asyncio
的核心是提供一个事件循环,用于异步编程。例如,当我们需要在不阻塞主线程的情况下进行请求时,就可以使用
asyncio
库。Python 3.4的
asyncio
模块包含事件循环、协程、未来对象(Futures)和任务等元素,用于I/O操作、网络编程等。
3.2 asyncio的组成元素
-
事件循环(Event loop)
:
asyncio模块允许每个进程有一个事件循环,它是异步编程的核心,负责调度和执行异步任务。 - 协程(Coroutines) :协程是一种特殊的生成器,遵循特定的约定。其最大的特点是在执行过程中可以暂停,等待外部处理(如I/O操作)完成后,再从暂停的位置继续执行。
- 未来对象(Futures) :表示一个尚未完成的进程,是一个在未来会有结果的对象,代表未完成的任务。
-
任务(Tasks)
:是
asyncio.Future的子类,用于封装和管理协程。可以使用asyncio.Task对象来封装一个协程。
3.3 使用asyncio的示例代码
以下是一个使用
asyncio
和
aiohttp
进行异步请求的示例代码:
#!/usr/local/bin/python3
import asyncio
from aiohttp import ClientSession
import time
async def fetch(url, session):
async with session.get(url) as response:
# async operation must be preceded by await
return await response.read()
async def execute(loop):
url = "http://httpbin.org/{}"
tasks = []
sites = ['headers','ip','user-agent']
# Fetch all responses within one Client session,
# keep connection alive for all requests.
async with ClientSession() as session:
for site in sites:
task = asyncio.ensure_future(fetch(url.format(site), session))
tasks.append(task)
# async operation must be preceded by await
responses = await asyncio.gather(*tasks)
# you now have all response bodies in this variable
for response in responses:
print(response.decode())
if __name__ == '__main__':
t1 = time.time()
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(execute(loop))
loop.run_until_complete(future)
print(time.time() - t1, 'seconds passed')
代码分析
-
fetch函数 :这是一个异步函数,用于向指定URL发送请求并读取响应内容。使用async with语句创建一个异步会话,并使用await关键字等待响应内容的读取。 -
execute函数 :创建一个异步会话,将多个请求任务添加到任务列表中,使用asyncio.gather方法等待所有任务完成,并打印每个响应的内容。 -
asyncio.ensure_future:用于将协程封装成任务。 -
loop.run_until_complete:事件循环运行直到指定的协程完成。 -
asyncio.gather:收集未来对象并等待它们全部完成。 -
await关键字 :告诉Python解释器,后续的表达式需要一些时间来计算,在此期间可以执行其他任务。
3.4 代码执行结果
运行上述代码后,输出结果如下:
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "Python/3.7 aiohttp/3.5.4"
}
}
{
"origin": "192.113.65.10, 192.113.65.10"
}
{
"user-agent": "Python/3.7 aiohttp/3.5.4"
}
0.4722881317138672 seconds passed
3.5 asyncio与其他方法的对比
| 方法 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
concurrent.futures.ThreadPoolExecutor
| 线程开销小,适合I/O密集型任务 | 受GIL限制,不适合CPU密集型任务 | 网络请求、文件读写等I/O密集型操作 |
multiprocessing
| 可以充分利用多核CPU,适合CPU密集型任务 | 进程开销大,进程间通信复杂 | 数据处理、计算密集型任务 |
asyncio
| 单线程异步编程,无线程和进程开销,适合高并发I/O场景 | 代码编写相对复杂,不适合CPU密集型任务 | 网络爬虫、异步服务器等高并发I/O场景 |
3.6 流程图
graph LR
A[开始] --> B[创建事件循环]
B --> C[创建异步任务]
C --> D[将任务添加到事件循环]
D --> E{任务是否完成}
E -- 是 --> F[获取结果]
E -- 否 --> D
F --> G[结束事件循环]
G --> H[结束]
4. 总结
通过前面的介绍,我们学习了多种构建服务器和实现异步编程的方法:
-
基于多进程的TCP服务器
:可以使用
concurrent.futures
模块的
ThreadPoolExecutor
和
ProcessPoolExecutor
,以及
multiprocessing
模块来实现多进程处理任务。
ThreadPoolExecutor
适合I/O密集型任务,
multiprocessing
适合CPU密集型任务。
-
基于asyncio和aiohttp的异步应用
:
asyncio
提供了事件循环、协程、未来对象和任务等元素,用于实现单线程的异步编程,适合高并发的I/O场景。
在实际应用中,需要根据任务的特点和需求选择合适的方法。例如,对于网络请求、文件读写等I/O密集型任务,可以优先考虑
ThreadPoolExecutor
或
asyncio
;对于数据处理、计算密集型任务,可以选择
multiprocessing
。同时,合理使用这些方法可以提高程序的性能和并发处理能力。
超级会员免费看
1427

被折叠的 条评论
为什么被折叠?



