当前位置:首页>python>Python异步编程常见问题解答:从原理到实战全解析

Python异步编程常见问题解答:从原理到实战全解析

  • 2026-06-30 20:00:10
Python异步编程常见问题解答:从原理到实战全解析

学习搭子,异步编程的学习过程中,咱们总会遇到各种困惑和疑问。有些问题看似简单,却触及异步编程的核心原理;有些问题则在实际开发中频繁出现。今天,咱们就集中解答这些常见问题,帮你彻底扫清学习障碍!

🎯 本文目标

通过本文,你将:

  1. 1. 解答异步编程学习中最常见的30个问题
  2. 2. 理清容易混淆的核心概念与原理
  3. 3. 掌握典型问题的解决方案与调试技巧
  4. 4. 避免常见的错误用法与性能陷阱
  5. 5. 建立系统的问题排查思路与方法

咱们的目标不仅是“知道答案”,更是要“理解为什么”,形成解决异步问题的系统性思维。

第一部分:基础概念与原理问答

Q1: 异步编程和多线程有什么区别?

核心区别

维度
异步编程
多线程
并发模型
协作式(单线程内切换)
抢占式(操作系统调度)
内存开销
极低(每个协程约2-4KB)
较高(每个线程约1-8MB)
切换成本
极低(用户态切换)
较高(内核态切换)
适用场景
I/O密集型任务
CPU密集型任务
调试难度
相对简单(确定性)
较难(竞态条件)
是否需要锁
不需要
通常需要

通俗理解

  • • 异步编程:一个聪明的服务员,在等待厨房做菜时,去服务其他客人
  • • 多线程:多个服务员同时工作,但可能争抢同一个资源(需要协调)

代码对比

# 异步版本
asyncdefasync_fetch(urls):
asyncwith aiohttp.ClientSession() as session:
        tasks = [session.get(url) for url in urls]
        responses = await asyncio.gather(*tasks)
return [await r.text() for r in responses]

# 多线程版本
defthreaded_fetch(urls):
with concurrent.futures.ThreadPoolExecutor() as executor:
        futures = [executor.submit(requests.get, url) for url in urls]
return [f.result().text for f in futures]

Q2: 协程到底是什么?和生成器有什么关系?

协程本质
协程是可暂停、可恢复的函数状态机,由事件循环在用户态调度执行。

与生成器的关系

  • • 历史渊源:Python的协程最初基于生成器实现(yield/yield from
  • • 语法演进:Python 3.5引入async/await语法,让协程成为一等公民
  • • 区别
    • • 生成器:主要用于生成值序列(生产者)
    • • 协程:主要用于消费值和控制流程(消费者)

底层联系

# 旧版(基于生成器)
@asyncio.coroutine
defold_style_coro():
yieldfrom asyncio.sleep(1)

# 新版(async/await)
asyncdefnew_style_coro():
await asyncio.sleep(1)

# 两者底层都使用生成器机制

Q3: GIL(全局解释器锁)对异步编程有什么影响?

影响分析

  1. 1. 对异步编程的积极影响
    • • 简化了协程调度,避免了线程安全复杂性
    • • 单线程内协程切换成本极低
    • • 无需锁机制,避免死锁风险
  2. 2. 局限性
    • • 无法利用多核进行CPU并行计算
    • • CPU密集型任务仍需多进程或其他方案

实际策略

import asyncio
import concurrent.futures

asyncdefhybrid_approach():
# I/O密集型:使用异步
asyncwith aiohttp.ClientSession() as session:
        data = await fetch_io_data(session)

# CPU密集型:使用进程池
    loop = asyncio.get_running_loop()
with concurrent.futures.ProcessPoolExecutor() as pool:
        result = await loop.run_in_executor(
            pool, 
            cpu_intensive_computation, 
            data
        )

return result

Q4: 为什么我的async函数没有执行?

常见原因

  1. 1. 忘记await
asyncdefmain():
    coro = async_function()  # 错误:只是创建协程对象
# 需要:result = await async_function()
  1. 2. 没有启动事件循环
# 缺少:asyncio.run(main())
asyncdefmain():
print("不会执行")

# 正确:asyncio.run(main())
  1. 3. 协程被创建但未调度
asyncdefmain():
# 仅创建,未await或create_task
    coro = asyncio.sleep(1)
# 需要:task = asyncio.create_task(coro)

诊断脚本

import asyncio

asyncdefdebug_async_execution():
print("Step 1: 函数开始")

# 模拟异步操作
await asyncio.sleep(0.1)
print("Step 2: 异步操作完成")

return"成功"

# 测试执行
try:
    result = asyncio.run(debug_async_execution())
print(f"结果: {result}")
except Exception as e:
print(f"错误: {e}")

Q5: async withasync for是什么?什么时候使用?

async with

  • • 用途:异步上下文管理器,用于异步资源的获取和释放
  • • 适用场景:数据库连接、HTTP会话、文件操作等
# 异步数据库连接
asyncwith aiomysql.create_pool(...) as pool:
asyncwith pool.acquire() as conn:
asyncwith conn.cursor() as cursor:
await cursor.execute("SELECT ...")
            result = await cursor.fetchall()

# 异步文件操作
asyncwith aiofiles.open('data.txt''r'as f:
    content = await f.read()

async for

  • • 用途:异步迭代器,用于异步生成序列
  • • 适用场景:数据库游标、网络流、实时数据源
# 异步数据库游标
asyncfor row in cursor:
    process(row)

# 自定义异步生成器
asyncdefasync_generator():
for i inrange(10):
await asyncio.sleep(0.1)
yield i

asyncdefmain():
asyncfor value in async_generator():
print(value)

第二部分:常见错误与调试问答

Q6: 为什么我的异步程序突然变慢?

排查步骤

  1. 1. 检查阻塞操作
import asyncio
import time

asyncdefslow_operation():
# 危险:同步阻塞调用
    time.sleep(1)  # 应该用 await asyncio.sleep(1)
return"完成"
  1. 2. 监控Task数量
asyncdefmonitor_tasks():
import asyncio

    tasks = asyncio.all_tasks()
    current = asyncio.current_task()

    active_tasks = [t for t in tasks if t isnot current]
print(f"活跃Task数: {len(active_tasks)}")

# 如果数量持续增长,可能有泄漏
  1. 3. 使用调试模式
asyncio.run(main(), debug=True)
# 会显示:慢回调警告、Task创建堆栈等

性能分析工具

import cProfile
import pstats

defprofile_async():
    asyncio.run(async_function())

cProfile.run('profile_async()''profile_stats')

stats = pstats.Stats('profile_stats')
stats.sort_stats('cumulative').print_stats(10)

Q7: 如何处理异步代码中的异常?

最佳实践

  1. 1. 在协程内部处理
asyncdefsafe_operation():
try:
        result = await risky_operation()
return result
except SpecificError as e:
# 处理特定异常
return fallback_value
except Exception as e:
# 记录日志
        logging.error(f"操作失败: {e}")
raise# 或返回默认值
  1. 2. 使用gather的return_exceptions
asyncdefmultiple_operations():
    tasks = [
        operation1(),
        operation2(),
        operation3()
    ]

# 不会因单个失败而整体失败
    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}")
  1. 3. Task级别的异常处理
asyncdefmonitored_task():
    task = asyncio.create_task(operation())

try:
        result = await task
return result
except Exception as e:
# 记录额外信息
print(f"Task失败: {task}, 异常: {e}")
raise

Q8: 如何调试死锁或长时间阻塞?

调试工具

  1. 1. asyncio调试模式
import asyncio

asyncdefdebug_deadlock():
# 启用调试
    asyncio.get_event_loop().set_debug(True)

# 设置慢回调阈值(默认100ms)
    asyncio.get_event_loop().slow_callback_duration = 0.05

# 你的代码...
  1. 2. 添加超时
import asyncio

asyncdefwith_timeout():
try:
# 设置超时
        result = await asyncio.wait_for(
            long_operation(),
            timeout=5.0
        )
return result
except asyncio.TimeoutError:
print("操作超时,可能死锁")
# 执行清理
returnNone
  1. 3. 日志记录
import asyncio
import logging

logging.basicConfig(
    level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(message)s'
)

asyncdeftraced_operation():
    logging.debug("开始操作")

await asyncio.sleep(1)

    logging.debug("操作完成")

Q9: 为什么asyncio.sleep(0)能让出控制权?

原理解释

asyncio.sleep(0)的特殊设计:

  1. 1. 创建一个立即到期的定时器
  2. 2. 将当前Task注册为定时器的回调
  3. 3. 协程在await点暂停
  4. 4. 事件循环发现定时器到期,将Task移回就绪队列
  5. 5. 下一轮调度时恢复执行

代码模拟

asyncdefsleep_zero():
# 内部实现简化
    future = asyncio.Future()

# 立即完成
    future.set_result(None)

# await让出控制权
returnawait future

实际用途

asyncdefcooperative_loop():
for i inrange(1000000):
# 定期让出,避免独占
if i % 1000 == 0:
await asyncio.sleep(0)

# 执行一些计算
        process_item(i)

Q10: 如何避免异步代码中的竞态条件?

常见场景与解决方案

  1. 1. 共享计数器
import asyncio

# 不安全版本
counter = 0

asyncdefunsafe_increment():
global counter
    temp = counter
await asyncio.sleep(0.001)  # 切换点
    counter = temp + 1

# 安全版本:使用锁
counter = 0
lock = asyncio.Lock()

asyncdefsafe_increment():
global counter
asyncwith lock:
        temp = counter
await asyncio.sleep(0.001)
        counter = temp + 1
  1. 2. 缓存失效问题
import asyncio

cache = {}
cache_lock = asyncio.Lock()

asyncdefget_cached_data(key):
# 双重检查锁模式(异步版本)
if key in cache:
return cache[key]

asyncwith cache_lock:
# 再次检查(其他任务可能已填充)
if key in cache:
return cache[key]

# 获取数据
        data = await fetch_data(key)
        cache[key] = data

return data
  1. 3. 使用异步队列
import asyncio

queue = asyncio.Queue(maxsize=100)

asyncdefproducer():
for i inrange(100):
await queue.put(i)
await asyncio.sleep(0.01)

asyncdefconsumer():
whileTrue:
        item = await queue.get()
        process(item)
        queue.task_done()

第三部分:性能优化问答

Q11: 异步程序CPU使用率100%怎么办?

原因与解决

  1. 1. 长时间无await的计算
asyncdefcpu_hog():
# 错误:大量计算无await
    result = 0
for i inrange(10**8):
        result += i * i

# 改进:分片计算
    result = 0
    chunk_size = 10000
for start inrange(010**8, chunk_size):
        end = min(start + chunk_size, 10**8)
        result += sum(i * i for i inrange(start, end))

# 定期让出
await asyncio.sleep(0)
  1. 2. 事件循环空转
# 改进:合理设置超时
asyncdefefficient_wait():
while has_work:
# 检查是否有任务
ifnot tasks:
# 短时间休眠,避免空转
await asyncio.sleep(0.01)
else:
await process_tasks()
  1. 3. 使用uvloop
import asyncio
import uvloop

# 替换默认事件循环
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

# 显著提升性能

Q12: 如何优化大量并发网络请求?

优化策略

  1. 1. 连接池与复用
import aiohttp

asyncdefoptimized_fetch(urls):
    connector = aiohttp.TCPConnector(
        limit=100,  # 最大连接数
        limit_per_host=10,  # 每主机最大连接
        ttl_dns_cache=300,  # DNS缓存时间
        force_close=False# 保持连接复用
    )

    timeout = aiohttp.ClientTimeout(
        total=30,
        connect=5,
        sock_read=10
    )

asyncwith aiohttp.ClientSession(
        connector=connector,
        timeout=timeout
    ) as session:
# 使用信号量控制并发
        semaphore = asyncio.Semaphore(50)

asyncdeffetch_one(url):
asyncwith semaphore:
asyncwith session.get(url) as resp:
returnawait resp.text()

        tasks = [asyncio.create_task(fetch_one(url)) 
for url in urls]

returnawait asyncio.gather(*tasks)
  1. 2. 批量处理
asyncdefbatch_requests(urls, batch_size=50):
    results = []

for i inrange(0len(urls), batch_size):
        batch = urls[i:i+batch_size]

# 并发处理批次
        batch_results = await asyncio.gather(
            *[fetch(url) for url in batch],
            return_exceptions=True
        )

        results.extend(batch_results)

# 批次间短暂暂停
await asyncio.sleep(0.1)

return results

Q13: 异步数据库连接如何优化?

最佳实践

  1. 1. 连接池配置
import aiomysql

asyncdefcreate_optimized_pool():
    pool = await aiomysql.create_pool(
        host='localhost',
        port=3306,
        user='user',
        password='pass',
        db='database',
        minsize=5,   # 最小连接数
        maxsize=20,  # 最大连接数
        echo=False,  # 关闭SQL日志
        autocommit=True,
        pool_recycle=3600,  # 连接回收时间
        max_queries=50000,  # 最大查询数后重建连接
        loop=asyncio.get_event_loop()
    )
return pool
  1. 2. 事务管理
asyncdefsafe_transaction(pool, user_id, amount):
asyncwith pool.acquire() as conn:
asyncwith conn.cursor() as cursor:
try:
# 开始事务
await conn.begin()

# 多个操作
await cursor.execute(
"UPDATE accounts SET balance = balance - %s WHERE id = %s",
                    (amount, user_id)
                )

await cursor.execute(
"INSERT INTO transactions VALUES (%s, %s, NOW())",
                    (user_id, amount)
                )

# 提交事务
await conn.commit()

except Exception as e:
# 回滚
await conn.rollback()
raise

Q14: 异步内存泄漏如何排查?

排查方法

  1. 1. 监控Task生命周期
import asyncio
import weakref

classTaskTracker:
def__init__(self):
self._tasks = weakref.WeakSet()

deftrack(self, task):
self._tasks.add(task)
return task

defactive_count(self):
returnlen(self._tasks)

# 使用
tracker = TaskTracker()

asyncdefmonitored_task():
    task = asyncio.create_task(operation())
    tracker.track(task)
returnawait task
  1. 2. 使用objgraph分析
import objgraph
import asyncio

asyncdefanalyze_memory():
# 记录当前对象
    objgraph.show_growth(limit=10)

# 运行一段时间后
await asyncio.sleep(60)

# 再次检查
    objgraph.show_growth(limit=10)

# 显示最多实例的类型
    objgraph.show_most_common_types(limit=20)
  1. 3. 垃圾回收诊断
import gc

asyncdefcheck_memory_leaks():
# 启用调试
    gc.set_debug(gc.DEBUG_SAVEALL)

# 强制回收
    collected = gc.collect()
print(f"回收对象: {collected}")

# 检查未回收对象
    unreachable = gc.garbage
print(f"未回收对象数: {len(unreachable)}")

if unreachable:
for obj in unreachable[:10]:
print(f"未回收对象: {type(obj)}, id: {id(obj)}")

第四部分:实战场景问答

Q15: 如何在现有同步项目中引入异步?

渐进式迁移策略

  1. 1. 从外围开始
# 1. 识别I/O密集型模块
# 2. 创建异步版本的服务
# 3. 通过线程池桥接

import asyncio
import concurrent.futures
from threading import Thread

classAsyncBridge:
def__init__(self):
self._loop = None
self._thread = None

defstart(self):
"""启动事件循环线程"""
self._loop = asyncio.new_event_loop()

defrun_loop():
            asyncio.set_event_loop(self._loop)
self._loop.run_forever()

self._thread = Thread(target=run_loop, daemon=True)
self._thread.start()

defstop(self):
ifself._loop:
self._loop.call_soon_threadsafe(self._loop.stop)

asyncdef_async_operation(self, data):
# 异步实现
await asyncio.sleep(0.1)
return processed_data

defsync_call(self, data):
"""同步调用异步操作"""
        future = asyncio.run_coroutine_threadsafe(
self._async_operation(data),
self._loop
        )
return future.result(timeout=10)
  1. 2. API层异步化
# 同步Flask → 异步FastAPI
from fastapi import FastAPI
import asyncio

app = FastAPI()

@app.get("/data")
asyncdefget_data():
# 异步数据库查询
    result = await async_db_query()
return result

# 原有同步代码通过线程池调用
@app.get("/legacy")
asyncdeflegacy_endpoint():
    loop = asyncio.get_running_loop()

# 将同步函数委托给线程池
    result = await loop.run_in_executor(
None,  # 默认线程池
        sync_legacy_function,
"参数"
    )

return result

Q16: 异步Web服务如何优雅关闭?

关闭流程

import asyncio
import signal
from contextlib import asynccontextmanager

classGracefulShutdown:
def__init__(self):
self.should_exit = False
self.active_connections = set()

defadd_connection(self, conn):
self.active_connections.add(conn)

defremove_connection(self, conn):
if conn inself.active_connections:
self.active_connections.remove(conn)

asyncdefwait_for_shutdown(self):
"""等待优雅关闭"""
# 停止接收新请求
print("停止接收新请求...")

# 等待活跃连接完成
ifself.active_connections:
print(f"等待 {len(self.active_connections)} 个活跃连接完成...")

# 设置超时
try:
await asyncio.wait_for(
self._wait_all_connections(),
                    timeout=30.0
                )
except asyncio.TimeoutError:
print("超时,强制关闭剩余连接")

# 执行清理
awaitself.cleanup_resources()

print("关闭完成")

asyncdefmain():
    shutdown = GracefulShutdown()

# 设置信号处理
    loop = asyncio.get_running_loop()

for sig in (signal.SIGINT, signal.SIGTERM):
        loop.add_signal_handler(
            sig,
lambda: asyncio.create_task(shutdown.wait_for_shutdown())
        )

# 运行服务...
await server.serve_forever()

Q17: 如何实现异步定时任务?

实现方案

  1. 1. 基础定时器
import asyncio

asyncdefperiodic_task(interval: float):
"""定期执行的任务"""
whileTrue:
print("执行定时任务...")
await asyncio.sleep(interval)

asyncdefscheduled_task(delay: float, func, *args):
"""延迟执行的任务"""
await asyncio.sleep(delay)
returnawait func(*args)

# 启动
asyncdefmain():
# 定期任务
    periodic = asyncio.create_task(periodic_task(5.0))

# 延迟任务
    result = await scheduled_task(10.0, some_async_function, "参数")

# 取消定期任务
    periodic.cancel()

try:
await periodic
except asyncio.CancelledError:
print("定期任务已取消")
  1. 2. 高级调度器
import asyncio
import heapq
from typing importCallableAny
from dataclasses import dataclass, field
from datetime import datetime, timedelta

@dataclass(order=True)
classScheduledJob:
    scheduled_time: datetime
    job_id: int = field(compare=False)
    func: Callable = field(compare=False)
    args: tuple = field(compare=False)
    kwargs: dict = field(compare=False)
    repeat: bool = False
    interval: timedelta = None

classAsyncScheduler:
def__init__(self):
self._jobs = []
self._next_id = 1
self._running = True
self._task = asyncio.create_task(self._run())

asyncdefschedule(self, func: Callable, delay: float
                      repeat: bool = False, interval: float = None,
                      *args, **kwargs
) -> int:
"""调度任务"""
        job_id = self._next_id
self._next_id += 1

        scheduled_time = datetime.now() + timedelta(seconds=delay)

if repeat and interval:
            interval_td = timedelta(seconds=interval)
else:
            interval_td = None

        job = ScheduledJob(
            scheduled_time=scheduled_time,
            job_id=job_id,
            func=func,
            args=args,
            kwargs=kwargs,
            repeat=repeat,
            interval=interval_td
        )

        heapq.heappush(self._jobs, job)
return job_id

asyncdefcancel(self, job_id: int) -> bool:
"""取消任务"""
for i, job inenumerate(self._jobs):
if job.job_id == job_id:
self._jobs.pop(i)
                heapq.heapify(self._jobs)
returnTrue
returnFalse

asyncdef_run(self):
"""调度器主循环"""
whileself._running:
ifnotself._jobs:
await asyncio.sleep(0.1)
continue

            now = datetime.now()
            next_job = self._jobs[0]

if now >= next_job.scheduled_time:
                job = heapq.heappop(self._jobs)

# 执行任务
                asyncio.create_task(self._execute_job(job))

# 如果需要重复,重新调度
if job.repeat and job.interval:
                    new_job = ScheduledJob(
                        scheduled_time=now + job.interval,
                        job_id=job.job_id,
                        func=job.func,
                        args=job.args,
                        kwargs=job.kwargs,
                        repeat=job.repeat,
                        interval=job.interval
                    )
                    heapq.heappush(self._jobs, new_job)
else:
# 等待到下一个任务
                wait_time = (next_job.scheduled_time - now).total_seconds()
await asyncio.sleep(min(wait_time, 0.1))

asyncdef_execute_job(self, job: ScheduledJob):
"""执行具体任务"""
try:
if asyncio.iscoroutinefunction(job.func):
await job.func(*job.args, **job.kwargs)
else:
# 同步函数使用线程池
                loop = asyncio.get_running_loop()
await loop.run_in_executor(
None,
                    job.func,
                    *job.args,
                    **job.kwargs
                )
except Exception as e:
print(f"任务执行失败: {e}")

asyncdefstop(self):
"""停止调度器"""
self._running = False
self._task.cancel()

try:
awaitself._task
except asyncio.CancelledError:
pass

Q18: 异步单元测试怎么写?

测试框架与模式

  1. 1. 使用pytest-asyncio
import pytest
import asyncio

# 基础测试
@pytest.mark.asyncio
asyncdeftest_async_function():
    result = await async_function()
assert result == "预期值"

# 模拟测试
@pytest.mark.asyncio
asyncdeftest_with_mock():
with patch('module.async_func'
               return_value="模拟值"):
        result = await target_function()
assert result == "预期结果"

# 超时测试
@pytest.mark.asyncio
asyncdeftest_timeout():
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(
            long_running_operation(),
            timeout=0.1
        )
  1. 2. 测试工具类
import asyncio
import unittest

classAsyncTestCase(unittest.TestCase):
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._loop = asyncio.new_event_loop()

defrun_async(self, coro):
"""运行异步函数"""
returnself._loop.run_until_complete(coro)

defsetUp(self):
        asyncio.set_event_loop(self._loop)

deftearDown(self):
self._loop.run_until_complete(
self._loop.shutdown_asyncgens()
        )
self._loop.close()
        asyncio.set_event_loop(None)

Q19: 异步日志记录的最佳实践?

异步日志方案

  1. 1. 使用structlog
import structlog
import asyncio

# 配置
structlog.configure(
    processors=[
        structlog.processors.TimeStamper(fmt="iso"),
        structlog.processors.JSONRenderer()
    ],
    context_class=dict,
    logger_factory=structlog.PrintLoggerFactory(),
    wrapper_class=structlog.BoundLogger,
    cache_logger_on_first_use=True,
)

asyncdefasync_logging_example():
    log = structlog.get_logger()

# 异步日志记录
await log.info("开始处理请求"
                   request_id="123"
                   user_id="456")

try:
        result = await process_request()
await log.info("请求处理成功",
                       request_id="123",
                       duration_ms=150)
return result
except Exception as e:
await log.error("请求处理失败",
                        request_id="123",
                        error=str(e),
                        traceback=True)
raise
  1. 2. 异步日志处理器
import logging
import asyncio
from concurrent.futures import ThreadPoolExecutor

classAsyncLogHandler(logging.Handler):
def__init__(self, level=logging.NOTSET):
super().__init__(level)
self._executor = ThreadPoolExecutor(max_workers=1)

asyncdefasync_emit(self, record):
"""异步处理日志记录"""
# 这里执行实际的日志记录(可能是I/O操作)
awaitself._write_log(record)

defemit(self, record):
"""同步接口,委托给异步处理"""
        loop = asyncio.get_event_loop()

# 将日志记录委托给线程池执行
        future = asyncio.run_coroutine_threadsafe(
self.async_emit(record),
            loop
        )

# 可选:等待完成或忽略
try:
            future.result(timeout=1.0)
except Exception:
pass# 日志记录失败不应影响主程序

第五部分:高级主题问答

Q20: 异步与同步代码混合时要注意什么?

混合编程守则

  1. 1. 单向调用原则
# 正确:异步调用同步(通过线程池)
asyncdefasync_calls_sync():
    loop = asyncio.get_running_loop()

# 将同步函数委托给线程池
    result = await loop.run_in_executor(
None,
        sync_function,
"参数"
    )
return result

# 危险:同步直接调用异步
defsync_calls_async():
# 错误做法:可能阻塞或死锁
    asyncio.run(async_function())

# 正确做法:使用专为同步环境设计的桥接
    result = asyncio.run(async_function())
return result
  1. 2. 资源隔离
import threading
import asyncio

classThreadLocalAsync:
"""每个线程有自己的事件循环"""

def__init__(self):
self._local = threading.local()

defget_loop(self):
"""获取或创建线程本地的事件循环"""
ifnothasattr(self._local, 'loop'):
self._local.loop = asyncio.new_event_loop()

returnself._local.loop

defrun_in_thread(self, coro_func, *args):
"""在调用线程中运行异步函数"""
        loop = self.get_loop()

ifnot loop.is_running():
# 在新线程中启动事件循环
defrun_loop():
                asyncio.set_event_loop(loop)
                loop.run_forever()

            thread = threading.Thread(target=run_loop, daemon=True)
            thread.start()

# 提交任务
        future = asyncio.run_coroutine_threadsafe(
            coro_func(*args),
            loop
        )

return future.result(timeout=30)

Q21: 异步编程的未来发展方向?

技术趋势

  1. 1. asyncio的持续改进
    • • Python 3.11+:更快的协程切换
    • • TaskGroup:更好的任务管理
    • • timeout上下文管理器:更简洁的超时处理
  2. 2. 异步生态的扩展
    • • 数据库:更多异步ORM和驱动
    • • 消息队列:完全异步的消息中间件
    • • 机器学习:异步训练和推理框架
  3. 3. 与其他技术栈的融合
    • • WebAssembly:异步运行时集成
    • • 边缘计算:轻量级异步运行时
    • • 微服务:异步服务网格

学习建议

# 跟踪最新特性
import sys

if sys.version_info >= (311):
# 使用TaskGroup等新特性
import asyncio

asyncdefmodern_async():
asyncwith asyncio.TaskGroup() as tg:
            task1 = tg.create_task(operation1())
            task2 = tg.create_task(operation2())

return task1.result(), task2.result()

Q22: 什么时候不应该使用异步?

不适用场景

  1. 1. 纯CPU密集型计算
    • • 矩阵运算、科学计算
    • • 图像/视频处理
    • • 机器学习模型训练
  2. 2. 简单脚本和工具
    • • 一次性数据处理脚本
    • • 简单的文件操作
    • • 不需要并发的简单任务
  3. 3. 已有同步架构
    • • 成熟的同步微服务架构
    • • 短期项目,异步收益不明显
    • • 团队缺乏异步经验

决策框架

defshould_use_async(scenario):
    criteria = {
'high_concurrent_io'True,  # 高并发I/O
'cpu_bound'False,          # CPU密集型
'existing_sync_infra'False# 已有同步架构
'team_experience'True,    # 团队有经验
'project_duration''long'# 项目周期
    }

if (criteria['high_concurrent_io'and
not criteria['cpu_bound'and
not criteria['existing_sync_infra']):
return"推荐异步"

return"考虑同步或混合方案"

第六部分:问题快速索引

按问题类型分类:

基础概念类

  • • Q1: 异步与多线程区别
  • • Q2: 协程与生成器关系
  • • Q3: GIL对异步的影响
  • • Q4: async函数不执行原因
  • • Q5: async with/for用法

错误调试类

  • • Q6: 异步程序变慢排查
  • • Q7: 异步异常处理
  • • Q8: 死锁调试方法
  • • Q9: asyncio.sleep(0)原理
  • • Q10: 避免竞态条件

性能优化类

  • • Q11: CPU使用率100%解决
  • • Q12: 并发网络请求优化
  • • Q13: 异步数据库优化
  • • Q14: 内存泄漏排查

实战场景类

  • • Q15: 同步项目引入异步
  • • Q16: 异步服务优雅关闭
  • • Q17: 异步定时任务实现
  • • Q18: 异步单元测试写法
  • • Q19: 异步日志记录实践

高级主题类

  • • Q20: 异步同步混合编程
  • • Q21: 异步未来发展方向
  • • Q22: 何时不用异步

快速解决方案表:

问题现象
可能原因
解决方案
程序突然变慢
阻塞操作、Task泄漏
检查同步调用、监控Task数量
内存持续增长
内存泄漏、缓存失控
使用objgraph分析、检查缓存策略
响应延迟增加
事件循环阻塞、调度失衡
启用调试模式、优化任务优先级
异常静默失败
忘记异常处理、gather设置
明确异常处理、使用return_exceptions
并发能力不足
连接数限制、资源竞争
调整并发控制、使用连接池

结语

学习搭子,异步编程的世界充满挑战,但掌握其原理和最佳实践后,你将能构建出高性能、高可靠的现代应用。

记住这些关键原则:

  1. 1. 理解原理:知道为什么,而不仅是怎么做
  2. 2. 规避陷阱:识别常见错误,提前预防
  3. 3. 持续优化:监控、分析、改进
  4. 4. 拥抱变化:跟踪最新发展,不断学习

异步不是终点,而是通往更高效、更可靠软件系统的桥梁。在实际项目中,要根据具体场景做出明智的选择:何时该用异步,何时该用同步,何时需要混合方案。

如果在实践中遇到新的问题,或者想深入探讨某个主题,随时可以继续交流。咱们一起在这个快速发展的技术领域里不断成长!

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-07-04 12:27:34 HTTP/2.0 GET : https://f.mffb.com.cn/a/488646.html
  2. 运行时间 : 0.113514s [ 吞吐率:8.81req/s ] 内存消耗:4,586.62kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=6d528ed641211a474fa22c0b13ff9889
  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.000599s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000811s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000331s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000285s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000503s ]
  6. SELECT * FROM `set` [ RunTime:0.000201s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000522s ]
  8. SELECT * FROM `article` WHERE `id` = 488646 LIMIT 1 [ RunTime:0.003340s ]
  9. UPDATE `article` SET `lasttime` = 1783139254 WHERE `id` = 488646 [ RunTime:0.005439s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000279s ]
  11. SELECT * FROM `article` WHERE `id` < 488646 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000425s ]
  12. SELECT * FROM `article` WHERE `id` > 488646 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.000605s ]
  13. SELECT * FROM `article` WHERE `id` < 488646 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.001194s ]
  14. SELECT * FROM `article` WHERE `id` < 488646 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.001835s ]
  15. SELECT * FROM `article` WHERE `id` < 488646 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.004322s ]
0.115091s