当前位置:首页>python>Python异步编程最佳实践:从常见误区到高效设计

Python异步编程最佳实践:从常见误区到高效设计

  • 2026-07-03 20:42:35
Python异步编程最佳实践:从常见误区到高效设计

学习搭子,咱们已经掌握了异步编程的基础原理和事件循环的内部机制。但“知道”不等于“会用”,在真实项目中,异步代码的陷阱往往隐藏在细节之中。今天咱们就来总结那些只有踩过坑才知道的宝贵经验,帮你避开常见误区,写出高效可靠的异步程序!

🎯 本文目标

通过本文,你将:

  1. 1. 识别异步编程中的十大常见陷阱及其表现
  2. 2. 掌握每个问题的根本原因与最佳解决方案
  3. 3. 学会在复杂场景中做出正确的架构决策
  4. 4. 建立异步代码的质量评估标准
  5. 5. 实战将最佳实践应用到现有项目中

咱们的目标不仅是“不出错”,更是要写出“高性能、可维护、易调试”的异步代码。

第一部分:异步编程的十大常见陷阱

陷阱1:在async函数中使用阻塞I/O

错误示例

import asyncioimport requests  # 同步HTTP库asyncdeffetch_data(url):# 错误的阻塞调用!    response = requests.get(url)  # 这会阻塞整个事件循环return response.text

问题分析

  • • requests.get()是同步函数,会阻塞当前线程
  • • 事件循环在等待网络响应时无法处理其他任务
  • • 高并发场景下性能急剧下降

正确做法

import aiohttp  # 异步HTTP库asyncdeffetch_data_correct(url):asyncwith aiohttp.ClientSession() as session:asyncwith session.get(url) as response:returnawait response.text()# 或者使用专门的异步适配器import httpx  # 支持同步和异步的HTTP库asyncdeffetch_with_httpx(url):asyncwith httpx.AsyncClient() as client:        response = await client.get(url)return response.text

核心原则绝不在async函数中调用同步阻塞I/O操作。如果必须使用同步库,将其委托给线程池。

陷阱2:忘记await关键字

错误示例

asyncdefprocess_data():    result = expensive_operation()  # 忘记await!return resultasyncdefexpensive_operation():await asyncio.sleep(1)return"计算结果"

问题表现

  • • 函数返回协程对象而非实际结果
  • • 后续操作触发AttributeError或类型错误
  • • 难以调试,因为代码“看起来”正确

正确做法

asyncdefprocess_data_correct():    result = await expensive_operation()  # 必须await!return result# 静态检查工具可以帮助发现这类问题# 使用mypy:pip install mypy# 运行:mypy --strict your_code.py

诊断技巧:如果看到<coroutine object ... at 0x...>,很可能忘记了await。

陷阱3:无限制创建Task

错误示例

asyncdefbatch_process(items):    tasks = []for item in items:# 危险:可能创建数千个Task!        task = asyncio.create_task(process_item(item))        tasks.append(task)returnawait asyncio.gather(*tasks)

问题分析

  • • 每个Task都有内存开销(约2-4KB)
  • • 大量Task会增加调度开销
  • • 可能导致内存耗尽或调度延迟

正确做法

import asynciofrom asyncio import Semaphoreasyncdefbatch_process_safe(items, max_concurrent=100):    semaphore = Semaphore(max_concurrent)asyncdefprocess_with_limit(item):asyncwith semaphore:returnawait process_item(item)    tasks = [asyncio.create_task(process_with_limit(item)) for item in items]returnawait asyncio.gather(*tasks)# 或者使用任务组(Python 3.11+)asyncdefbatch_process_with_taskgroup(items):    results = []asyncwith asyncio.TaskGroup() as tg:for item in items:            task = tg.create_task(process_item(item))            results.append(task)return [task.result() for task in results]

经验法则:并发Task数量不应超过核心数的10-100倍,具体取决于任务类型。

陷阱4:忽略异常处理

错误示例

asyncdefrisky_operation():await asyncio.sleep(1)raise ValueError("意外错误")asyncdefmain():    task = asyncio.create_task(risky_operation())# 异常被静默吞没!await asyncio.sleep(2)

问题表现

  • • 协程异常不立即抛出,可能被忽略
  • • 调试时难以定位问题根源
  • • 可能导致资源泄漏或状态不一致

正确做法

asyncdefmain_correct():    task = asyncio.create_task(risky_operation())try:await taskexcept ValueError as e:print(f"捕获异常: {e}")# 执行清理操作except Exception as e:print(f"未预期异常: {e}")# 记录日志并优雅降级# 使用gather的return_exceptions参数asyncdefmultiple_tasks():    tasks = [        safe_operation(),        risky_operation(),        another_operation()    ]# 不会因单个失败而整体失败    results = await asyncio.gather(*tasks, return_exceptions=True)for i, result inenumerate(results):ifisinstance(result, Exception):print(f"任务{i}失败: {result}")else:print(f"任务{i}成功: {result}")

黄金法则永远为每个异步操作提供明确的异常处理。

陷阱5:协程中执行长时间CPU计算

错误示例

asyncdefheavy_computation():    result = 0for i inrange(10**7):  # 大量循环计算        result += i * ireturn result

问题分析

  • • Python的GIL使CPU计算无法并行
  • • 长时间占用CPU会阻塞事件循环
  • • 其他异步任务无法及时响应

正确做法

import asynciofrom concurrent.futures import ProcessPoolExecutordefcpu_bound_calculation(n):"""CPU密集型同步函数"""returnsum(i * i for i inrange(n))asyncdefheavy_computation_async():# 使用进程池隔离计算    loop = asyncio.get_running_loop()with ProcessPoolExecutor() as pool:        result = await loop.run_in_executor(            pool,             cpu_bound_calculation, 10**7        )return result# 或者使用C扩展释放GIL# import c_extension_moduleasyncdefcompute_with_c_extension():# 假设c_compute在C层释放了GIL    result = await asyncio.to_thread(c_extension_module.c_compute, 10**7)return result

优化策略:将CPU计算分片,定期用await asyncio.sleep(0)让出控制权。

陷阱6:错误的超时处理

错误示例

asyncdefunreliable_operation():# 模拟不稳定的网络请求if random.random() < 0.3:await asyncio.sleep(10)  # 长时间挂起return"结果"asyncdefmain():try:# 超时设置不合理        result = await asyncio.wait_for(unreliable_operation(), timeout=5)except asyncio.TimeoutError:# 但Task还在后台运行!print("超时了")

问题:超时后原始Task仍在运行,可能导致资源泄漏。

正确做法

asyncdefmain_correct():    task = asyncio.create_task(unreliable_operation())try:        result = await asyncio.wait_for(task, timeout=5)return resultexcept asyncio.TimeoutError:# 取消未完成的Task        task.cancel()try:await task  # 等待取消完成except asyncio.CancelledError:print("任务已取消")# 提供备用方案return fallback_operation()

进阶技巧:为不同类型的操作设置不同的超时策略。

陷阱7:忽视协程取消的副作用

错误示例

asyncdefcritical_operation():try:await long_running_task()except asyncio.CancelledError:# 直接重新抛出,没有清理raiseasyncdeflong_running_task():# 获取资源但可能被中断    resource = acquire_resource()await asyncio.sleep(10)  # 可能在这里被取消# 如果被取消,资源永远不会释放!

正确做法

asyncdefcritical_operation_safe():    resource = Nonetry:        resource = acquire_resource()await long_running_task_with_cleanup(resource)except asyncio.CancelledError:# 执行必要的清理if resource:            release_resource(resource)# 可以选择重新抛出或处理取消print("操作被取消,已清理资源")raise# 或者 return Nonefinally:# 确保资源最终被释放if resource:            release_resource(resource)asyncdeflong_running_task_with_cleanup(resource):try:await asyncio.sleep(10)except asyncio.CancelledError:# 在这里也可以进行清理await cleanup_intermediate_state(resource)raise

重要原则:取消是协作式的,必须处理资源清理。

陷阱8:事件循环的线程安全问题

错误示例

import asyncioimport threadingdeffrom_another_thread():    loop = asyncio.get_event_loop()# 错误:从非所有者线程调用    future = asyncio.run_coroutine_threadsafe(        async_function(),         loop    )    result = future.result()  # 可能阻塞asyncdefasync_function():return"结果"

问题:asyncio的API大多不是线程安全的,错误使用会导致竞争条件。

正确做法

import asyncioimport threadingclassThreadSafeAsyncBridge:def__init__(self, loop):self.loop = loopself._callbacks = []defcall_async(self, coro_func, *args):"""从任何线程安全地调用异步函数"""        future = asyncio.run_coroutine_threadsafe(            coro_func(*args), self.loop        )return futuredefschedule_callback(self, callback, *args):"""调度回调到事件循环线程"""self.loop.call_soon_threadsafe(callback, *args)# 使用示例asyncdefmain():    loop = asyncio.get_running_loop()    bridge = ThreadSafeAsyncBridge(loop)# 从其他线程使用bridge    thread = threading.Thread(        target=run_in_thread,         args=(bridge,)    )    thread.start()defrun_in_thread(bridge):    future = bridge.call_async(async_task, "参数")    result = future.result(timeout=5)  # 阻塞等待结果print(f"从线程获取结果: {result}")

最佳实践:使用call_soon_threadsafe()run_coroutine_threadsafe()进行跨线程通信。

陷阱9:忽略异步上下文管理器

错误示例

asyncdefprocess_file_unsafe():    file = open("data.txt""r")  # 同步打开# 如果在await期间被取消...    content = await process_content(file.read())    file.close()  # 可能永远不会执行!

正确做法

import aiofiles  # 异步文件操作库asyncdefprocess_file_safe():# 使用异步上下文管理器asyncwith aiofiles.open("data.txt""r"as file:        content = await file.read()        result = await process_content(content)return result# 自动关闭,即使发生异常# 自定义异步上下文管理器classAsyncDatabaseConnection:def__init__(self, dsn):self.dsn = dsnself.conn = Noneasyncdef__aenter__(self):self.conn = await create_async_connection(self.dsn)returnself.connasyncdef__aexit__(self, exc_type, exc_val, exc_tb):ifself.conn:awaitself.conn.close()# 可以选择处理异常return exc_type isNone# 如果返回True,异常被抑制# 使用示例asyncdefquery_database():asyncwith AsyncDatabaseConnection("postgresql://..."as conn:        result = await conn.execute("SELECT ...")return result

核心优势:异步上下文管理器确保资源的正确获取和释放,即使在异常或取消情况下。

陷阱10:不当的日志记录

错误示例

import loggingimport timelogging.basicConfig(level=logging.INFO)asyncdefprocess_request():    start = time.time()# 大量日志可能阻塞事件循环    logging.info(f"开始处理请求: {request_id}")    result = await expensive_operation()# 同步日志调用    logging.info(f"请求完成,耗时: {time.time() - start:.2f}s")return result

问题:同步的日志记录可能成为性能瓶颈,特别是在高频率日志场景。

正确做法

import loggingimport asynciofrom datetime import datetimeclassAsyncLogger:def__init__(self, name):self.logger = logging.getLogger(name)self._queue = asyncio.Queue()self._worker_task = asyncio.create_task(self._log_worker())asyncdefinfo(self, message):"""异步记录日志"""awaitself._queue.put(("INFO", message, datetime.now()))asyncdeferror(self, message, exc_info=None):awaitself._queue.put(("ERROR", message, datetime.now(), exc_info))asyncdef_log_worker(self):whileTrue:try:                record = awaitself._queue.get()                level, message, timestamp = record[:3]# 同步调用但控制在后台线程self.logger.log(getattr(logging, level),f"[{timestamp}{message}"                )self._queue.task_done()except asyncio.CancelledError:breakexcept Exception as e:# 日志记录器自身的异常处理print(f"日志记录失败: {e}")asyncdefclose(self):# 等待队列清空awaitself._queue.join()self._worker_task.cancel()try:awaitself._worker_taskexcept asyncio.CancelledError:pass# 使用结构化日志import structlogasyncdefstructured_logging_example():    logger = structlog.get_logger()# 结构化日志,更容易分析和过滤await logger.info("请求处理开始"                     request_id="123"                     user_id="456"                     endpoint="/api/data")try:        result = await process_request()await logger.info("请求处理成功",                         request_id="123",                         duration_ms=150)return resultexcept Exception as e:await logger.error("请求处理失败",                          request_id="123",                          error=str(e),                          traceback=True)raise

高级技巧:使用结构化日志,便于后续的日志分析和监控。

第二部分:异步架构设计原则

原则1:分层设计与关注点分离

正确的架构分层

应用层 (Application Layer)├── API路由和请求处理├── 业务逻辑编排└── 响应序列化服务层 (Service Layer)├── 业务逻辑实现├── 数据验证和转换└── 事务管理数据访问层 (Data Access Layer)├── 数据库操作(异步ORM)├── 缓存操作└── 外部API调用基础设施层 (Infrastructure Layer)├── 配置管理├── 日志记录└── 监控指标

实现示例

# 数据访问层classUserRepository:def__init__(self, db_session):self.db = db_sessionasyncdefget_by_id(self, user_id):returnawaitself.db.execute(            select(User).where(User.id == user_id)        )asyncdefsave(self, user):self.db.add(user)awaitself.db.commit()# 服务层classUserService:def__init__(self, user_repo, email_service):self.repo = user_repoself.email = email_serviceasyncdefregister_user(self, user_data):# 业务逻辑        user = User(**user_data)awaitself.repo.save(user)# 异步发送欢迎邮件(不阻塞主流程)        asyncio.create_task(self.email.send_welcome_email(user.email)        )return user# 应用层classUserController:def__init__(self, user_service):self.service = user_serviceasyncdefregister(self, request):        data = await request.json()        user = awaitself.service.register_user(data)return json_response({"id": user.id})

关键好处

  • • 各层职责明确,易于测试
  • • 可以独立替换实现(如切换数据库)
  • • 避免业务逻辑与技术细节耦合

原则2:基于消息的松耦合通信

同步紧耦合 vs 异步松耦合

# 错误:同步紧耦合asyncdeforder_processing():# 直接调用各种服务await inventory_service.reserve_items()await payment_service.charge_customer()await shipping_service.schedule_delivery()# 问题:一个服务失败影响整体# 正确:基于消息的松耦合asyncdeforder_processing_async():# 发布事件,不等待结果await event_bus.publish(OrderCreated(        order_id=order.id,        items=order.items,        customer_id=order.customer_id    ))# 各服务独立处理事件# - 库存服务:监听OrderCreated,预留库存# - 支付服务:监听OrderCreated,发起扣款# - 物流服务:监听PaymentCompleted,安排发货

消息总线实现

import asynciofrom typing importDictListCallableAnyfrom dataclasses import dataclass@dataclassclassEvent:typestr    data: AnyclassAsyncEventBus:def__init__(self):self._listeners: Dict[strList[Callable]] = {}self._queue = asyncio.Queue()self._worker_task = asyncio.create_task(self._process_events())defsubscribe(self, event_type: str, callback: Callable):"""订阅特定类型事件"""if event_type notinself._listeners:self._listeners[event_type] = []self._listeners[event_type].append(callback)asyncdefpublish(self, event: Event):"""发布事件(异步)"""awaitself._queue.put(event)asyncdef_process_events(self):whileTrue:try:                event = awaitself._queue.get()if event.typeinself._listeners:# 并行调用所有监听器                    tasks = [                        asyncio.create_task(callback(event.data))for callback inself._listeners[event.type]                    ]# 等待所有监听器完成,但不因单个失败而停止await asyncio.gather(*tasks, return_exceptions=True)self._queue.task_done()except asyncio.CancelledError:breakexcept Exception as e:print(f"事件处理错误: {e}")

优势

  • • 服务间解耦,独立演化
  • • 更好的故障隔离
  • • 支持事件溯源和重放

原则3:可观察性与可调试性设计

关键监控指标

import asyncioimport timefrom collections import defaultdictfrom dataclasses import dataclassfrom typing importDictListOptional@dataclassclassAsyncMetrics:    task_count: int = 0    active_tasks: int = 0    completed_tasks: int = 0    failed_tasks: int = 0    avg_task_duration: float = 0.0    event_loop_busy_time: float = 0.0classAsyncMonitor:def__init__(self):self.metrics = AsyncMetrics()self._task_start_times: Dict[asyncio.Task, float] = {}self._task_durations: List[float] = []self._loop_start_time: Optional[float] = Nonedeftask_created(self, task: asyncio.Task):"""监控Task创建"""self.metrics.task_count += 1self.metrics.active_tasks += 1self._task_start_times[task] = time.monotonic()# 添加完成回调        task.add_done_callback(self._task_completed)def_task_completed(self, task: asyncio.Task):"""Task完成回调"""self.metrics.active_tasks -= 1if task inself._task_start_times:            duration = time.monotonic() - self._task_start_times[task]self._task_durations.append(duration)# 更新平均耗时            total = sum(self._task_durations)self.metrics.avg_task_duration = total / len(self._task_durations)delself._task_start_times[task]if task.exception():self.metrics.failed_tasks += 1else:self.metrics.completed_tasks += 1asyncdefcollect_metrics(self):"""收集性能指标"""        loop = asyncio.get_running_loop()# 监控事件循环负载        busy_start = time.monotonic()# 执行一些测量操作await asyncio.sleep(0.1)        busy_end = time.monotonic()self.metrics.event_loop_busy_time = busy_end - busy_startreturnself.metricsdefget_report(self) -> Dict:"""生成监控报告"""return {"total_tasks"self.metrics.task_count,"active_tasks"self.metrics.active_tasks,"task_success_rate": (self.metrics.completed_tasks / max(1self.metrics.task_count) * 100            ),"avg_duration_ms"self.metrics.avg_task_duration * 1000,"loop_busy_percent": (self.metrics.event_loop_busy_time / max(0.1, time.monotonic() - (self._loop_start_time or0)) * 100            )        }# 使用装饰器自动监控defmonitor_async_performance(func):asyncdefwrapper(*args, **kwargs):        monitor = AsyncMonitor()# 创建Task时自动监控        task = asyncio.create_task(func(*args, **kwargs))        monitor.task_created(task)try:            result = await taskreturn resultfinally:# 记录最终指标            report = monitor.get_report()print(f"性能报告: {report}")return wrapper

集成到应用中

import aiohttpfrom aiohttp import webimport jsonasyncdefmetrics_endpoint(request):"""提供监控指标的API端点"""    monitor = request.app['monitor']    report = monitor.get_report()return web.json_response(report)asyncdefbackground_monitoring(app):"""后台监控任务"""    monitor = app['monitor']whileTrue:await asyncio.sleep(60)  # 每分钟收集一次        metrics = await monitor.collect_metrics()# 发送到监控系统await send_to_metrics_backend(metrics)# 检查健康状态if metrics.failed_tasks > 10:            alert_system("高失败率警告")defcreate_app():    app = web.Application()    app['monitor'] = AsyncMonitor()# 添加监控端点    app.router.add_get('/metrics', metrics_endpoint)# 启动后台监控    app.on_startup.append(start_background_monitoring)return app

监控价值

  • • 快速发现性能瓶颈
  • • 预警系统故障
  • • 支持容量规划和优化

第三部分:实战代码审查指南

审查清单:异步代码质量评估

1. 阻塞操作检查

# 危险模式检测import reblocking_patterns = [r'requests\.(get|post|put|delete)',r'time\.sleep\(',r'open\(',r'\.read\(\)',r'\.write\(\)',r'subprocess\.run\(',]defcheck_blocking_calls(code: str) -> List[str]:"""检测代码中的阻塞调用"""    issues = []for pattern in blocking_patterns:        matches = re.findall(pattern, code)if matches:            issues.append(f"发现阻塞调用: {pattern}")return issues

2. await遗漏检测

import astclassAwaitChecker(ast.NodeVisitor):def__init__(self):self.missing_awaits = []defvisit_Await(self, node):# 检查await表达式passdefvisit_Call(self, node):# 检测可能的协程调用未awaitifisinstance(node.func, ast.Name):            func_name = node.func.id# 简单的启发式规则if func_name.startswith('async_'or func_name.endswith('_async'):# 检查父节点是否是awaitifnotisinstance(self.current_parent, ast.Await):self.missing_awaits.append((                        node.lineno, f"可能忘记await: {func_name}()"                    ))self.generic_visit(node)

3. 资源泄漏检查

asyncdefcheck_resource_leaks():"""检查潜在的资源泄漏"""import gc# 收集当前Task数量    tasks = [t for t in asyncio.all_tasks() if t isnot asyncio.current_task()]print(f"当前活跃Task数: {len(tasks)}")# 检查未关闭的文件描述符import psutil    process = psutil.Process()    open_files = process.open_files()    connections = process.connections()print(f"打开文件数: {len(open_files)}")print(f"网络连接数: {len(connections)}")# 建议清理iflen(tasks) > 100:print("警告:可能Task泄漏")iflen(open_files) > 50:print("警告:可能文件描述符泄漏")

代码审查实战:改进示例

原始代码(存在问题)

import asyncioimport requestsasyncdefprocess_user_requests(users):    results = []for user in users:# 问题1:使用同步requests        profile = requests.get(f"https://api.example.com/users/{user.id}")# 问题2:未处理可能异常        orders = requests.get(f"https://api.example.com/orders/{user.id}")# 问题3:计算耗时操作可能阻塞        analysis = analyze_user_data(profile, orders)        results.append(analysis)return resultsdefanalyze_user_data(profile, orders):# 同步CPU密集型计算# 大量数据处理...return"分析结果"

改进后代码

import asyncioimport aiohttpfrom typing importListDictAnyfrom concurrent.futures import ProcessPoolExecutorclassUserRequestProcessor:def__init__(self, max_concurrent=50):self.max_concurrent = max_concurrentself._session = Noneself._process_pool = ProcessPoolExecutor()asyncdef__aenter__(self):# 异步初始化资源self._session = aiohttp.ClientSession(            connector=aiohttp.TCPConnector(                limit=self.max_concurrent,                ttl_dns_cache=300            ),            timeout=aiohttp.ClientTimeout(total=10)        )returnselfasyncdef__aexit__(self, exc_type, exc_val, exc_tb):# 确保资源清理ifself._session:awaitself._session.close()self._process_pool.shutdown(wait=True)asyncdefprocess_user_requests(self, users: List[Dict]) -> List[Any]:"""改进版本:异步处理用户请求"""        semaphore = asyncio.Semaphore(self.max_concurrent)asyncdefprocess_single_user(user: Dict):asyncwith semaphore:try:# 异步并行获取数据                    profile_future = self._fetch_user_profile(user['id'])                    orders_future = self._fetch_user_orders(user['id'])                    profile, orders = await asyncio.gather(                        profile_future,                        orders_future,                        return_exceptions=True                    )# 处理可能的异常ifisinstance(profile, Exception):raise profileifisinstance(orders, Exception):raise orders# 将CPU计算委托给进程池                    analysis = await asyncio.get_running_loop().run_in_executor(self._process_pool,self._analyze_user_data,                        profile,                        orders                    )return analysisexcept Exception as e:# 记录异常但继续处理其他用户awaitself._log_error(f"处理用户失败: {user['id']}, 错误: {e}")returnNone# 并发处理所有用户        tasks = [asyncio.create_task(process_single_user(user)) for user in users]        results = await asyncio.gather(*tasks, return_exceptions=True)# 过滤掉None结果return [r for r in results if r isnotNone]asyncdef_fetch_user_profile(self, user_id: str):"""异步获取用户资料"""asyncwithself._session.get(f"https://api.example.com/users/{user_id}"        ) as response:            response.raise_for_status()returnawait response.json()asyncdef_fetch_user_orders(self, user_id: str):"""异步获取用户订单"""asyncwithself._session.get(f"https://api.example.com/orders/{user_id}"        ) as response:            response.raise_for_status()returnawait response.json()def_analyze_user_data(self, profile: Dict, orders: Dict) -> Any:"""CPU密集型分析(同步函数)"""# 实际分析逻辑...return {"user_id": profile.get('id'),"profile_summary": summarize_profile(profile),"order_stats": calculate_order_stats(orders),"risk_score": assess_risk(profile, orders)        }asyncdef_log_error(self, message: str):"""异步记录错误"""# 实现日志记录逻辑print(f"[ERROR] {message}")# 使用示例asyncdefmain():    users = [{"id""1"}, {"id""2"}, {"id""3"}]asyncwith UserRequestProcessor(max_concurrent=10as processor:        results = await processor.process_user_requests(users)print(f"处理完成: {len(results)}个结果")for result in results:print(f"结果: {result}")

第四部分:持续学习与社区资源

推荐学习路径

1. 基础巩固

  • • 官方文档:asyncio — Asynchronous I/O
  • • 核心概念:A Conceptual Overview of asyncio
  • • 最佳实践:Async IO in Python: A Complete Walkthrough

2. 深入进阶

  • • 源码分析:CPython asyncio源码
  • • 性能优化:High-performance asyncio
  • • 架构设计:Designing Data-Intensive Applications(异步系统相关章节)

3. 实战项目

  • • 开源项目学习:aio-libs生态
  • • 实际挑战:Advent of Code(使用异步解决)
  • • 系统构建:从零实现异步Web框架、爬虫引擎、消息队列

社区与工具

核心社区

  • • Python官方论坛:async-sig邮件列表
  • • Stack Overflowasyncio标签
  • • GitHub:关注aio-libs组织

调试工具

# 内置调试工具asyncio.run(main(), debug=True)# 第三方工具# uvloop:高性能替代# aiomonitor:实时监控# async-timeout:上下文管理器超时

性能分析

# 使用py-spy进行性能分析pip install py-spypy-spy top --pid <PID># 使用cProfile分析异步代码python -m cProfile -o profile.prof async_script.pysnakeviz profile.prof

结语

学习搭子,异步编程的旅程既有挑战也有乐趣。记住这些最佳实践,不仅能帮你避免常见陷阱,更能让你写出高效、可靠、易维护的异步代码。

异步不是终点,而是通往高性能系统的桥梁。随着你对异步编程理解的深入,你将能够:

  1. 1. 设计更优雅的系统架构:基于消息和事件驱动
  2. 2. 解决更复杂的并发问题:从容应对高并发场景
  3. 3. 构建更高性能的应用:充分发挥硬件潜力
  4. 4. 培养更系统的思维模式:从局部优化到全局设计

学习之路永无止境,但每一次深入理解,都会让你在技术道路上走得更稳、更远。如果在实践中遇到新的问题或有趣的发现,随时可以继续交流。

咱们一起加油,在异步编程的世界里不断探索前行!

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-07-04 09:33:52 HTTP/2.0 GET : https://f.mffb.com.cn/a/488367.html
  2. 运行时间 : 0.104562s [ 吞吐率:9.56req/s ] 内存消耗:4,597.41kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=2ed1b8cffdc60c6f3fbbd91b3f7cae2e
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000547s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000814s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000376s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000324s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000706s ]
  6. SELECT * FROM `set` [ RunTime:0.000269s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000690s ]
  8. SELECT * FROM `article` WHERE `id` = 488367 LIMIT 1 [ RunTime:0.000967s ]
  9. UPDATE `article` SET `lasttime` = 1783128832 WHERE `id` = 488367 [ RunTime:0.012307s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000454s ]
  11. SELECT * FROM `article` WHERE `id` < 488367 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000655s ]
  12. SELECT * FROM `article` WHERE `id` > 488367 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.004659s ]
  13. SELECT * FROM `article` WHERE `id` < 488367 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.000675s ]
  14. SELECT * FROM `article` WHERE `id` < 488367 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.006629s ]
  15. SELECT * FROM `article` WHERE `id` < 488367 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.005957s ]
0.106218s