{"id":80363,"date":"2026-03-04T22:11:11","date_gmt":"2026-03-04T14:11:11","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/80363.html"},"modified":"2026-03-04T22:11:11","modified_gmt":"2026-03-04T14:11:11","slug":"%e4%b8%8d%e5%90%8cdjango%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%92%8c%e9%83%a8%e7%bd%b2%e6%96%b9%e5%bc%8f%e7%9a%84%e6%80%a7%e8%83%bd%e8%b0%83%e7%a0%94","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/80363.html","title":{"rendered":"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814"},"content":{"rendered":"<h3 id=\"\u6d4b\u8bd5\u73af\u5883\">\u6d4b\u8bd5\u73af\u5883#<\/h3>\n<p>\u672c\u6b21\u6d4b\u8bd5\u7684\u9879\u76ee\u90e8\u7f72\u5728\u817e\u8baf\u4e912C2G\u7684\u5c0f\u6c34\u7ba1\u670d\u52a1\u5668\u4e0a&#xff0c;\u611f\u89c9\u670d\u52a1\u5668\u6027\u80fd\u4e25\u91cd\u62d6\u7d2f\u4e86\u5e94\u7528\u6027\u80fd\u54c8\u54c8&#x1f923;<\/p>\n<p>\u800c\u4e14\u6211\u8fd8\u53d1\u73b0\u4e00\u4e2a\u4e8b\u60c5&#xff0c;\u817e\u8baf\u4e91\u4f3c\u4e4e\u5077\u5077\u6478\u6478\u5728\u9ad8\u5cf0\u671f&#xff08;\u5982\u4e0b\u5348&#xff09;\u628a\u670d\u52a1\u5668\u6027\u80fd\u964d\u4f4e&#xff0c;\u4e0b\u5348\u548c\u51cc\u6668\u6d4b\u8bd5\u7684\u7ed3\u679c\u6709\u5f88\u5927\u5dee\u5f02\u3002<\/p>\n<p>\u672c\u6b21\u53c2\u4e0e\u6d4b\u8bd5\u7684wsgi\/asgi\u670d\u52a1\u5668\u6709 daphne, granian, gunicorn, uwsgi, uvicorn, hypercorn \u57fa\u672c\u6db5\u76d6\u4e86 python \u751f\u6001\u7684\u5927\u90e8\u5206\u670d\u52a1\u5668\u4e86~<\/p>\n<p>\u672c\u6b21\u4f7f\u7528 wrk \u4f5c\u4e3a\u6027\u80fd\u6d4b\u8bd5\u529f\u80fd&#xff0c;\u6240\u6709\u63a5\u53e3\u90fd\u4f7f\u7528\u00a0-t4 -c200 -d30s\u00a0\u7684\u6d4b\u8bd5\u53c2\u6570\u3002<\/p>\n<h3 id=\"\u6027\u80fd\u6d4b\u8bd5\u7ed3\u8bba\">\u6027\u80fd\u6d4b\u8bd5\u7ed3\u8bba#<\/h3>\n<h4 id=\"rps-\u6392\u540d\">RPS \u6392\u540d#<\/h4>\n<table>\n<tr>\u6392\u540dserverreq\/sec\u63a5\u53e3<\/tr>\n<tbody>\n<tr>\n<td>&#x1f947; 1<\/td>\n<td>uWSGI &#043; WSGI<\/td>\n<td>1206<\/td>\n<td>WSGI<\/td>\n<\/tr>\n<tr>\n<td>&#x1f948; 2<\/td>\n<td>Gunicorn &#043; WSGI<\/td>\n<td>740<\/td>\n<td>WSGI<\/td>\n<\/tr>\n<tr>\n<td>&#x1f949; 3<\/td>\n<td>Granian &#043; ASGI<\/td>\n<td>220<\/td>\n<td>ASGI<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Daphne &#043; ASGI<\/td>\n<td>190<\/td>\n<td>ASGI<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>Uvicorn &#043; ASGI<\/td>\n<td>181<\/td>\n<td>ASGI<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>Hypercorn &#043; ASGI<\/td>\n<td>168<\/td>\n<td>ASGI<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ed3\u8bba&#xff1a;<\/p>\n<ul>\n<li>\n<p>WSGI \u6574\u4f53\u8fdc\u5feb\u4e8e ASGI<\/p>\n<p>\u8fd9\u662f\u9884\u671f\u7ed3\u679c&#xff0c;Django \u7684\u5185\u90e8\u539f\u751f\u5c31\u662f WSGI&#xff0c;\u540c\u6b65\u8def\u7531\u4e0d\u9700\u8981\u989d\u5916\u7684 async \u8f6c\u6362\u3002<\/p>\n<\/li>\n<li>\n<p>uWSGI \u518d\u6b21\u8bc1\u660e\u81ea\u5df1\u662f WSGI \u6027\u80fd\u4e4b\u738b<\/p>\n<p>uWSGI \u7684 C \u5b9e\u73b0\u3001\u6210\u719f\u7684 worker \u7ba1\u7406\u3001\u5185\u5b58\u5229\u7528\u4f18\u5316\u90fd\u8ba9\u5b83\u5728\u7eaf WSGI \u573a\u666f\u65e0\u654c\u3002<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"\u5185\u5b58\u5360\u7528\u5bf9\u6bd4\">\u5185\u5b58\u5360\u7528\u5bf9\u6bd4#<\/h4>\n<table>\n<tr>\u6392\u540dserver\u5185\u5b58<\/tr>\n<tbody>\n<tr>\n<td>&#x1f947; 1<\/td>\n<td>uWSGI<\/td>\n<td>58M&#xff08;\u6781\u4f4e&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Granian<\/td>\n<td>170M<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Daphne<\/td>\n<td>175M<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Hypercorn<\/td>\n<td>300M<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>Gunicorn&#043;WSGI<\/td>\n<td>430M<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>Uvicorn<\/td>\n<td>500M&#xff08;\u6700\u9ad8&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ed3\u8bba&#xff1a;<\/p>\n<ul>\n<li>uWSGI \u4e0d\u4ec5\u6700\u5feb&#xff0c;\u8fd8\u6700\u7701\u5185\u5b58<\/li>\n<li>Uvicorn \u5185\u5b58\u6700\u9ad8&#xff08;Python \u5355 worker overhead \u660e\u663e&#xff09;<\/li>\n<li>ASGI \u670d\u52a1\u5668\u666e\u904d\u5185\u5b58\u504f\u9ad8\u662f\u6b63\u5e38\u73b0\u8c61<\/li>\n<\/ul>\n<h3 id=\"\u6027\u80fd\u6d4b\u8bd5\u6570\u636e\">\u6027\u80fd\u6d4b\u8bd5\u6570\u636e#<\/h3>\n<p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6027\u80fd\u6d4b\u8bd5\u6570\u636e\u3002<\/p>\n<h4 id=\"daphne--asgi\">daphne &#043; asgi#<\/h4>\n<p>\u6d4b\u8bd5\u65f6\u5185\u5b58\u5cf0\u503c\u5360\u7528175M\u5de6\u53f3<\/p>\n<p>$ wrk -t4 -c200 -d30s http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency     1.03s   134.59ms   1.99s    89.71%<br \/>\n    Req\/Sec    69.17     64.53   313.00     80.49%<br \/>\n  5737 requests in 30.04s, 2.14MB read<br \/>\n  Socket errors: connect 0, read 0, write 0, timeout 4<br \/>\nRequests\/sec:    190.99<br \/>\nTransfer\/sec:     72.93KB<\/p>\n<h4 id=\"granian--asgi\">granian &#043; asgi#<\/h4>\n<p>\u6d4b\u8bd5\u65f6\u5185\u5b58\u5cf0\u503c\u5360\u7528170M\u5de6\u53f3<\/p>\n<p>$ curl http:\/\/127.0.0wrk -t4 -c200 -d30s875\/api\/django-starter\/monitoring\/health<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency   886.48ms   79.27ms   1.26s    85.40%<br \/>\n    Req\/Sec    86.07    112.58   490.00     84.05%<br \/>\n  6637 requests in 30.04s, 2.72MB read<br \/>\nRequests\/sec:    220.93<br \/>\nTransfer\/sec:     92.56KB<\/p>\n<h4 id=\"gunicorn--wsgi\">gunicorn &#043; wsgi#<\/h4>\n<p>\u6d4b\u8bd5\u65f6\u5185\u5b58\u5cf0\u503c\u5360\u7528430M\u5de6\u53f3<\/p>\n<p>$ wrk -t4 -c200 -d30s http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency   268.67ms   31.82ms 425.23ms   86.13%<br \/>\n    Req\/Sec   187.47    119.54   494.00     56.66%<br \/>\n  22244 requests in 30.03s, 9.52MB read<br \/>\nRequests\/sec:    740.60<br \/>\nTransfer\/sec:    324.74KB<\/p>\n<h4 id=\"uwsgi--wsgi\">uwsgi &#043; wsgi#<\/h4>\n<p>\u6d4b\u8bd5\u65f6\u5185\u5b58\u5cf0\u503c\u5360\u752858M\u5de6\u53f3<\/p>\n<p>$ wrk -t4 -c200 -d30s http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency   199.52ms  277.85ms   1.97s    87.56%<br \/>\n    Req\/Sec   302.89     88.84   575.00     67.33%<br \/>\n  36210 requests in 30.02s, 12.95MB read<br \/>\n  Socket errors: connect 0, read 36216, write 0, timeout 96<br \/>\nRequests\/sec:   1206.08<br \/>\nTransfer\/sec:    441.68KB<\/p>\n<h4 id=\"uvicorn--asgi\">uvicorn &#043; asgi#<\/h4>\n<p>\u6d4b\u8bd5\u65f6\u5185\u5b58\u5cf0\u503c\u5360\u7528500M\u5de6\u53f3<\/p>\n<p>$ wrk -t4 -c200 -d30s http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency     1.00s   537.23ms   2.00s    59.81%<br \/>\n    Req\/Sec    62.04     66.05   460.00     83.03%<br \/>\n  5440 requests in 30.04s, 2.23MB read<br \/>\n  Socket errors: connect 0, read 0, write 0, timeout 369<br \/>\nRequests\/sec:    181.07<br \/>\nTransfer\/sec:     76.05KB<\/p>\n<h4 id=\"hypercorn--asgi\">hypercorn &#043; asgi#<\/h4>\n<p>\u6d4b\u8bd5\u65f6\u5185\u5b58\u5cf0\u503c\u5360\u7528300M\u5de6\u53f3<\/p>\n<p>$ wrk -t4 -c200 -d30s http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9875\/api\/django-starter\/monitoring\/health<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency   734.64ms  423.05ms   2.00s    51.70%<br \/>\n    Req\/Sec    55.53     52.50   360.00     83.49%<br \/>\n  5064 requests in 30.06s, 2.09MB read<br \/>\n  Socket errors: connect 0, read 0, write 0, timeout 1124<br \/>\nRequests\/sec:    168.45<br \/>\nTransfer\/sec:     71.24KB<\/p>\n<h4 id=\"\u66f4\u7ec6\u5316\u7684\u6027\u80fd\u6d4b\u8bd5\">\u66f4\u7ec6\u5316\u7684\u6027\u80fd\u6d4b\u8bd5#<\/h4>\n<p>\u8bbf\u95ee\u4e00\u4e2a\u6709\u6570\u636e\u5e93\u67e5\u8be2\u7684\u63a5\u53e3\u00a0http:\/\/127.0.0.1:9878\/api\/demo\/movie\/movie<\/p>\n<p>\u76f4\u63a5 uwsgi<\/p>\n<p>$ wrk -t4 -c200 -d30s http:\/\/127.0.0.1:9875\/api\/demo\/movie\/movie<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9875\/api\/demo\/movie\/movie<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency   501.93ms  260.82ms   1.99s    94.20%<br \/>\n    Req\/Sec    61.02     39.11   191.00     62.02%<br \/>\n  6726 requests in 30.04s, 15.84MB read<br \/>\n  Socket errors: connect 0, read 6758, write 0, timeout 45<br \/>\nRequests\/sec:    223.89<br \/>\nTransfer\/sec:    539.82KB<\/p>\n<p>\u7ecf\u8fc7 caddy \u53cd\u4ee3<\/p>\n<p>$ wrk -t4 -c200 -d30s http:\/\/127.0.0.1:9878\/api\/demo\/movie\/movie<br \/>\nRunning 30s test &#064; http:\/\/127.0.0.1:9878\/api\/demo\/movie\/movie<br \/>\n  4 threads and 200 connections<br \/>\n  Thread Stats   Avg      Stdev     Max   &#043;\/- Stdev<br \/>\n    Latency   562.46ms  166.56ms   1.65s    88.77%<br \/>\n    Req\/Sec    52.66     21.07   150.00     65.63%<br \/>\n  6299 requests in 30.04s, 15.06MB read<br \/>\n  Socket errors: connect 0, read 0, write 0, timeout 208<br \/>\n  Non-2xx or 3xx responses: 40<br \/>\nRequests\/sec:    209.69<br \/>\nTransfer\/sec:    513.30KB<\/p>\n<h3 id=\"\u4ee3\u7801\">\u4ee3\u7801#<\/h3>\n<p>\u6765\u4ecb\u7ecd\u4e0b\u4ee3\u7801\u7684\u90e8\u5206\u3002<\/p>\n<p>\u8fd9\u6b21\u62ff\u6765\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u7684\u4e3b\u8981\u662f\u5065\u5eb7\u68c0\u67e5\u548c\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u5e93\u8bbf\u95ee\u63a5\u53e3\u3002\u6240\u6709\u63a5\u53e3\u90fd\u662f\u7528 django-ninja \u5f00\u53d1\u7684\u3002<\/p>\n<h4 id=\"\u6570\u636e\u5e93\u8bbf\u95ee\u63a5\u53e3\">\u6570\u636e\u5e93\u8bbf\u95ee\u63a5\u53e3#<\/h4>\n<p>\u6d89\u53ca\u5230\u6570\u636e\u5e93\u7684\u5c31\u662f\u8fd9\u4e2a\u63a5\u53e3<\/p>\n<p>\u67e5\u8be2\u6570\u636e\u8fd4\u56de&#043;\u5206\u9875\u529f\u80fd&#xff0c;\u975e\u5e38\u7b80\u5355<\/p>\n<p>&#064;router.get(&#039;\/movie&#039;, response&#061;List[MovieOut], url_name&#061;&#039;demo\/movie\/list&#039;)<br \/>\n&#064;paginate<br \/>\ndef list_items(request):<br \/>\n    qs &#061; Movie.objects.all()<br \/>\n    return qs<\/p>\n<h4 id=\"\u5065\u5eb7\u68c0\u67e5\u63a5\u53e3\">\u5065\u5eb7\u68c0\u67e5\u63a5\u53e3#<\/h4>\n<p>\u5f15\u5165\u8fd9\u4e9bpackage<\/p>\n<p>import asyncio<br \/>\nfrom ninja import Router<br \/>\nfrom django.db import connections<br \/>\nfrom django.db.utils import OperationalError<br \/>\nfrom redis import Redis<br \/>\nfrom redis.exceptions import RedisError<br \/>\nfrom redis import asyncio as aioredis<br \/>\nimport os<br \/>\nimport time<br \/>\nimport platform<br \/>\nimport subprocess<br \/>\nimport anyio<\/p>\n<p>\u539f\u7248\u7684\u63a5\u53e3\u662f\u7eaf\u540c\u6b65\u7684&#xff0c;\u8fd8\u8981\u8c03\u7528\u4e00\u4e2a\u7cfb\u7edf\u8fdb\u7a0b\u53bb\u83b7\u53d6 uptime&#xff0c;\u8fd9\u4e2a\u64cd\u4f5c\u4e25\u91cd\u62d6\u6162\u4e86\u6574\u4e2a\u63a5\u53e3\u7684\u901f\u5ea6&#xff0c;\u6211\u8fd9\u6b21\u91cd\u6784\u6210\u540c\u6b65\u548c\u5f02\u6b65\u4e24\u79cd\u63a5\u53e3\u3002<\/p>\n<p>\u9996\u5148\u662f\u6700\u7b80\u5355\u7684\u7248\u672c&#xff0c;\u8fd9\u91cc\u9762\u5c31\u5565\u4e5f\u6ca1\u6709&#xff0c;\u5355\u7eaf\u8fd4\u56de JSON\u3002<\/p>\n<p>&#064;router.get(&#039;health&#039;)<br \/>\ndef simple_health_check(request):<br \/>\n    &#034;&#034;&#034;\u5065\u5eb7\u68c0\u67e5\u7aef\u70b9&#xff0c;\u7528\u4e8e\u5bb9\u5668\u5065\u5eb7\u68c0\u67e5\u548c\u76d1\u63a7&#034;&#034;&#034;<br \/>\n    response_data &#061; {<br \/>\n        &#039;status&#039;: &#039;healthy&#039;,<br \/>\n        &#039;status_code&#039;: 200,<br \/>\n    }<\/p>\n<p>    return response_data<\/p>\n<h4 id=\"\u5f02\u6b65\u63a5\u53e3\">\u5f02\u6b65\u63a5\u53e3#<\/h4>\n<p>\u5148\u6765\u5f02\u6b65\u63a5\u53e3\u3002<\/p>\n<p>\u5176\u5b9e\u6211\u5f88\u5c11\u7528\u5230 python \u7684\u5f02\u6b65\u529f\u80fd&#xff0c;\u53ef\u80fd\u8fd9\u4e5f\u548c python \u5bf9\u5f02\u6b65\u7684\u652f\u6301\u6bd4\u8f83\u5dee\u6709\u5173\u7cfb\u3002<\/p>\n<p>\u5148\u5199\u4e24\u4e2a\u5f02\u6b65\u65b9\u6cd5&#xff0c;\u7528\u6765\u68c0\u67e5\u6570\u636e\u5e93\u548cRedis\u8fde\u63a5\u3002<\/p>\n<p>async def check_db_async():<br \/>\n    &#034;&#034;&#034;\u6570\u636e\u5e93\u68c0\u67e5&#xff08;\u540c\u6b65 ORM \u2192 \u653e\u7ebf\u7a0b\u6c60&#xff09;&#034;&#034;&#034;<br \/>\n    try:<br \/>\n        def _check():<br \/>\n            for conn in connections.all():<br \/>\n                conn.cursor()<br \/>\n            return True<\/p>\n<p>        return await anyio.to_thread.run_sync(_check)<br \/>\n    except OperationalError:<br \/>\n        return False<\/p>\n<p>async def check_redis_async():<br \/>\n    &#034;&#034;&#034;Redis \u5f02\u6b65\u68c0\u67e5&#xff08;\u6ce8\u610f&#xff1a;\u786e\u4fdd\u5173\u95ed\u5ba2\u6237\u7aef\u4ee5\u91ca\u653e\u8fde\u63a5\u6c60&#xff09;&#034;&#034;&#034;<br \/>\n    try:<br \/>\n        redis_client &#061; aioredis.Redis(<br \/>\n            host&#061;&#034;redis&#034;,<br \/>\n            port&#061;6379,<br \/>\n            socket_connect_timeout&#061;1,<br \/>\n            decode_responses&#061;True,<br \/>\n        )<br \/>\n        try:<br \/>\n            ok &#061; await redis_client.ping()<br \/>\n            return bool(ok)<br \/>\n        finally:<br \/>\n            # \u5173\u95ed\u5ba2\u6237\u7aef\u548c\u8fde\u63a5\u6c60&#xff0c;\u907f\u514d\u8fde\u63a5\u6cc4\u9732&#xff08;\u5728\u77ed\u751f\u547d\u5468\u671f\u7684\u5bb9\u5668\/\u8bf7\u6c42\u91cc\u5f88\u91cd\u8981&#xff09;<br \/>\n            try:<br \/>\n                await redis_client.close()<br \/>\n            except Exception:<br \/>\n                pass<br \/>\n            try:<br \/>\n                await redis_client.connection_pool.disconnect()<br \/>\n            except Exception:<br \/>\n                pass<br \/>\n    except Exception:<br \/>\n        return False<\/p>\n<p>Django ORM \u4e0d\u652f\u6301\u5f02\u6b65&#xff0c;\u6240\u4ee5\u8fd9\u91cc\u7528 anyio \u8fd9\u4e2a\u5305\u6765\u7b80\u5316\u4e00\u4e0b\u64cd\u4f5c&#xff0c;\u628a\u540c\u6b65\u64cd\u4f5c\u5305\u88c5\u4e00\u4e0b\u5047\u88c5\u6210\u5f02\u6b65\u8fd0\u884c\u3002\u5b9e\u9645\u4e0a\u7528 asyncio \u8fd9\u4e2a\u5e93\u4e5f\u80fd\u505a&#xff0c;\u4e0d\u8fc7 anyio \u65b9\u4fbf\u4e00\u70b9\u3002<\/p>\n<p>\u5728\u7528 aioredis \u65f6&#xff0c;\u8fd9\u91cc\u6709\u4e2a\u5751&#xff0c;\u6211\u4e00\u5f00\u59cb\u5b89\u88c5\u4e86 aioredis \u8fd9\u4e2a\u5305&#xff0c;\u7ed3\u679c\u53d1\u73b0\u548c\u539f\u6709\u7684 redis \u5305\u51b2\u7a81\u4e86\u3002<\/p>\n<p>\u67e5\u770b\u6587\u6863\u624d\u53d1\u73b0\u00a0from redis import asyncio as aioredis\u00a0\u5c31\u5b8c\u4e8b\u513f\u4e86&#xff0c;redis \u5305\u91cc\u5df2\u7ecf\u81ea\u5e26\u4e86\u5f02\u6b65\u652f\u6301\u3002<\/p>\n<p>\u63a5\u7740\u5b9e\u73b0\u5f02\u6b65\u63a5\u53e3<\/p>\n<p>&#064;router.get(&#039;health\/async&#039;)<br \/>\nasync def health_check_async(request):<br \/>\n    &#034;&#034;&#034;Async \u6a21\u5f0f\u5065\u5eb7\u68c0\u67e5&#xff08;ASGI \u4f18\u5316\u7248&#xff09;&#034;&#034;&#034;<br \/>\n    # \u4f7f\u7528 asyncio.gather \u5e76\u53d1\u8fd0\u884c DB \u548c Redis \u68c0\u67e5<br \/>\n    db_ok, redis_ok &#061; await asyncio.gather(<br \/>\n        check_db_async(),<br \/>\n        check_redis_async(),<br \/>\n    )<\/p>\n<p>    status &#061; &#034;healthy&#034; if db_ok and redis_ok else &#034;unhealthy&#034;<br \/>\n    status_code &#061; 200 if status &#061;&#061; &#034;healthy&#034; else 503<\/p>\n<p>    return {<br \/>\n        &#034;status&#034;: status,<br \/>\n        &#034;status_code&#034;: status_code,<br \/>\n        &#034;checks&#034;: {<br \/>\n            &#034;database&#034;: &#034;ok&#034; if db_ok else &#034;error&#034;,<br \/>\n            &#034;redis&#034;: &#034;ok&#034; if redis_ok else &#034;error&#034;,<br \/>\n        },<br \/>\n        &#034;system&#034;: get_system_info(),<br \/>\n    }<\/p>\n<h4 id=\"\u540c\u6b65\u63a5\u53e3\">\u540c\u6b65\u63a5\u53e3#<\/h4>\n<p>\u540c\u6b65\u7684\u5c31\u7b80\u5355\u4e86<\/p>\n<p>&#064;router.get(&#039;health\/sync&#039;)<br \/>\ndef health_check_sync(request):<br \/>\n    &#034;&#034;&#034;\u540c\u6b65\u7248\u672c\u7684\u5065\u5eb7\u68c0\u67e5\u63a5\u53e3&#034;&#034;&#034;<\/p>\n<p>    # &#8212; \u68c0\u67e5\u6570\u636e\u5e93 &amp; Redis &#8212;<br \/>\n    db_ok &#061; check_db_sync()<br \/>\n    redis_ok &#061; check_redis_sync()<\/p>\n<p>    # &#8212; \u7cfb\u7edf\u4fe1\u606f &#8212;<br \/>\n    system_info &#061; {<br \/>\n        &#034;timestamp&#034;: time.time(),<br \/>\n        &#034;uptime&#034;: get_uptime(),<br \/>\n        &#034;hostname&#034;: os.environ.get(&#034;HOSTNAME&#034;, &#034;&#034;),<br \/>\n        &#034;environment&#034;: os.environ.get(&#034;ENVIRONMENT&#034;, &#034;development&#034;),<br \/>\n        &#034;os&#034;: platform.system(),<br \/>\n    }<\/p>\n<p>    # &#8212; \u72b6\u6001\u7801 &#8212;<br \/>\n    status &#061; &#034;healthy&#034; if db_ok and redis_ok else &#034;unhealthy&#034;<br \/>\n    status_code &#061; 200 if status &#061;&#061; &#034;healthy&#034; else 503<\/p>\n<p>    # &#8212; \u8fd4\u56de\u6570\u636e &#8212;<br \/>\n    return {<br \/>\n        &#034;status&#034;: status,<br \/>\n        &#034;status_code&#034;: status_code,<br \/>\n        &#034;checks&#034;: {<br \/>\n            &#034;database&#034;: &#034;ok&#034; if db_ok else &#034;error&#034;,<br \/>\n            &#034;redis&#034;: &#034;ok&#034; if redis_ok else &#034;error&#034;,<br \/>\n        },<br \/>\n        &#034;system&#034;: system_info,<br \/>\n    }<\/p>\n<h3 id=\"\u5c0f\u7ed3\">\u5c0f\u7ed3#<\/h3>\n<p>\u4f20\u7edf\u5e94\u7528\u65e0\u8111\u9009 uwsgi \u5c31\u5bf9\u4e86\u3002<\/p>\n<p>\u9700\u8981\u5f02\u6b65\u7684\u53ef\u4ee5\u628a\u5f02\u6b65\u90a3\u90e8\u5206\u5207\u5272\u51fa\u6765\u7528 granian \u6216\u8005 daphne \u8dd1\u3002<\/p>\n<p>\u5f53\u7136\u8fd9\u4e5f\u589e\u52a0\u4e86\u590d\u6742\u5ea6&#xff0c;\u4f46\u8fd9\u662f\u5bf9\u6027\u80fd\u4f18\u5316\u7684\u6700\u4f73\u6743\u8861\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d4b\u8bd5\u73af\u5883#<br \/>\n\u672c\u6b21\u6d4b\u8bd5\u7684\u9879\u76ee\u90e8\u7f72\u5728\u817e\u8baf\u4e912C2G\u7684\u5c0f\u6c34\u7ba1\u670d\u52a1\u5668\u4e0a&#xff0c;\u611f\u89c9\u670d\u52a1\u5668\u6027\u80fd\u4e25\u91cd\u62d6\u7d2f\u4e86\u5e94\u7528\u6027\u80fd\u54c8\u54c8&#x1f923;<br \/>\n\u800c\u4e14\u6211\u8fd8\u53d1\u73b0\u4e00\u4e2a\u4e8b\u60c5&#xff0c;\u817e\u8baf\u4e91\u4f3c\u4e4e\u5077\u5077\u6478\u6478\u5728\u9ad8\u5cf0\u671f&#xff08;\u5982\u4e0b\u5348&#xff09;\u628a\u670d\u52a1\u5668\u6027\u80fd\u964d\u4f4e&#xff0c;\u4e0b\u5348\u548c\u51cc\u6668\u6d4b\u8bd5\u7684\u7ed3\u679c\u6709\u5f88\u5927\u5dee\u5f02\u3002<br \/>\n\u672c\u6b21\u53c2\u4e0e\u6d4b\u8bd5\u7684wsgi\/asgi\u670d\u52a1\u5668\u6709 daphne, granian, gunicorn, uwsgi, uvicorn, hypercorn \u57fa\u672c\u6db5\u76d6\u4e86 python <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[890,2547,43],"topic":[],"class_list":["post-80363","post","type-post","status-publish","format-standard","hentry","category-server","tag-django","tag-sqlite","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/80363.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6d4b\u8bd5\u73af\u5883# \u672c\u6b21\u6d4b\u8bd5\u7684\u9879\u76ee\u90e8\u7f72\u5728\u817e\u8baf\u4e912C2G\u7684\u5c0f\u6c34\u7ba1\u670d\u52a1\u5668\u4e0a&#xff0c;\u611f\u89c9\u670d\u52a1\u5668\u6027\u80fd\u4e25\u91cd\u62d6\u7d2f\u4e86\u5e94\u7528\u6027\u80fd\u54c8\u54c8&#x1f923; \u800c\u4e14\u6211\u8fd8\u53d1\u73b0\u4e00\u4e2a\u4e8b\u60c5&#xff0c;\u817e\u8baf\u4e91\u4f3c\u4e4e\u5077\u5077\u6478\u6478\u5728\u9ad8\u5cf0\u671f&#xff08;\u5982\u4e0b\u5348&#xff09;\u628a\u670d\u52a1\u5668\u6027\u80fd\u964d\u4f4e&#xff0c;\u4e0b\u5348\u548c\u51cc\u6668\u6d4b\u8bd5\u7684\u7ed3\u679c\u6709\u5f88\u5927\u5dee\u5f02\u3002 \u672c\u6b21\u53c2\u4e0e\u6d4b\u8bd5\u7684wsgi\/asgi\u670d\u52a1\u5668\u6709 daphne, granian, gunicorn, uwsgi, uvicorn, hypercorn \u57fa\u672c\u6db5\u76d6\u4e86 python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/80363.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-04T14:11:11+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80363.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/80363.html\",\"name\":\"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-04T14:11:11+00:00\",\"dateModified\":\"2026-03-04T14:11:11+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/80363.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/80363.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80363.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/80363.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6d4b\u8bd5\u73af\u5883# \u672c\u6b21\u6d4b\u8bd5\u7684\u9879\u76ee\u90e8\u7f72\u5728\u817e\u8baf\u4e912C2G\u7684\u5c0f\u6c34\u7ba1\u670d\u52a1\u5668\u4e0a&#xff0c;\u611f\u89c9\u670d\u52a1\u5668\u6027\u80fd\u4e25\u91cd\u62d6\u7d2f\u4e86\u5e94\u7528\u6027\u80fd\u54c8\u54c8&#x1f923; \u800c\u4e14\u6211\u8fd8\u53d1\u73b0\u4e00\u4e2a\u4e8b\u60c5&#xff0c;\u817e\u8baf\u4e91\u4f3c\u4e4e\u5077\u5077\u6478\u6478\u5728\u9ad8\u5cf0\u671f&#xff08;\u5982\u4e0b\u5348&#xff09;\u628a\u670d\u52a1\u5668\u6027\u80fd\u964d\u4f4e&#xff0c;\u4e0b\u5348\u548c\u51cc\u6668\u6d4b\u8bd5\u7684\u7ed3\u679c\u6709\u5f88\u5927\u5dee\u5f02\u3002 \u672c\u6b21\u53c2\u4e0e\u6d4b\u8bd5\u7684wsgi\/asgi\u670d\u52a1\u5668\u6709 daphne, granian, gunicorn, uwsgi, uvicorn, hypercorn \u57fa\u672c\u6db5\u76d6\u4e86 python","og_url":"https:\/\/www.wsisp.com\/helps\/80363.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-04T14:11:11+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/80363.html","url":"https:\/\/www.wsisp.com\/helps\/80363.html","name":"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-04T14:11:11+00:00","dateModified":"2026-03-04T14:11:11+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/80363.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/80363.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/80363.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4e0d\u540cDjango\u670d\u52a1\u5668\u548c\u90e8\u7f72\u65b9\u5f0f\u7684\u6027\u80fd\u8c03\u7814"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=80363"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80363\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=80363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=80363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=80363"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=80363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}