{"id":818,"date":"2025-01-11T10:09:17","date_gmt":"2025-01-11T02:09:17","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=818"},"modified":"2025-01-11T10:09:17","modified_gmt":"2025-01-11T02:09:17","slug":"%e8%a6%81%e6%a0%bc%e5%a4%96%e5%85%b3%e6%b3%a8%e8%bf%99%e4%ba%9bpython%e5%8f%af%e8%83%bd%e9%81%87%e5%88%b0%e7%9a%84%e7%bb%86%e8%8a%82%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/11\/%e8%a6%81%e6%a0%bc%e5%a4%96%e5%85%b3%e6%b3%a8%e8%bf%99%e4%ba%9bpython%e5%8f%af%e8%83%bd%e9%81%87%e5%88%b0%e7%9a%84%e7%bb%86%e8%8a%82%e9%97%ae%e9%a2%98\/","title":{"rendered":"\u8981\u683c\u5916\u5173\u6ce8\u8fd9\u4e9bPython\u53ef\u80fd\u9047\u5230\u7684\u7ec6\u8282\u95ee\u9898"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u5e38\u89c1\u7684 Python \u7f16\u7a0b\u95ee\u9898\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u8f83\u4e3a\u4e0d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u662f\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u3001\u7279\u5b9a\u73af\u5883\u6216\u8005\u590d\u6742\u5e94\u7528\u4e2d\u51fa\u73b0\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e0d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u6309\u7c7b\u522b\u8fdb\u884c\u5206\u7c7b\u6574\u7406\uff0c\u5e2e\u52a9\u4f60\u6df1\u5165\u4e86\u89e3 Python \u7f16\u7a0b\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u7ec6\u8282\u95ee\u9898\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u6027\u80fd\u76f8\u5173\u7684\u4e0d\u5e38\u89c1\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\uff08\u9ad8\u5185\u5b58\u6cc4\u6f0f\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u5e94\u7528\u7a0b\u5e8f\u957f\u65f6\u95f4\u8fd0\u884c\u65f6\uff0c\u67d0\u4e9b\u5bf9\u8c61\u53ef\u80fd\u5728\u4e0d\u518d\u4f7f\u7528\u65f6\u4ecd\u7136\u88ab\u5f15\u7528\uff0c\u5bfc\u81f4\u5185\u5b58\u4e0d\u65ad\u589e\u52a0\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>gc<\/code> \u6a21\u5757\u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536\uff0c\u67e5\u770b\u662f\u5426\u5b58\u5728\u65e0\u6cd5\u56de\u6536\u7684\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528 <code>tracemalloc<\/code> \u8ddf\u8e2a\u5185\u5b58\u5206\u914d\u7684\u6765\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import tracemalloc\n  tracemalloc.start()\n  # \u8fd0\u884c\u4ee3\u7801\n  snapshot = tracemalloc.take_snapshot()\n  top_stats = snapshot.statistics('lineno')<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <strong>\u51fd\u6570\u8c03\u7528\u6808\u8fc7\u6df1\uff08\u6808\u6ea2\u51fa\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u9012\u5f52\u6df1\u5ea6\u8fc7\u5927\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\u6216\u8d85\u51fa\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u51cf\u5c11\u9012\u5f52\u6df1\u5ea6\u6216\u8f6c\u7528\u8fed\u4ee3\u65b9\u6cd5\u3002\u5982\u679c\u9012\u5f52\u662f\u5fc5\u8981\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 Python \u7684\u9012\u5f52\u9650\u5236\uff0c\u589e\u52a0\u6700\u5927\u9012\u5f52\u6df1\u5ea6\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import sys\n  sys.setrecursionlimit(10000)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.3 <strong><code>__del__<\/code> \u65b9\u6cd5\u5f15\u8d77\u7684\u5185\u5b58\u6cc4\u6f0f<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__del__<\/code> \u65b9\u6cd5\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u81ea\u52a8\u8c03\u7528\uff0c\u7136\u800c\u5b83\u53ef\u80fd\u4e0e\u5783\u573e\u56de\u6536\u673a\u5236\u53d1\u751f\u51b2\u7a81\uff0c\u7279\u522b\u662f\u5728\u5b58\u5728\u5faa\u73af\u5f15\u7528\u65f6\uff0c\u5bfc\u81f4\u65e0\u6cd5\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u907f\u514d\u5728 <code>__del__<\/code> \u4e2d\u6267\u884c\u590d\u6742\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528 <code>weakref<\/code> \u6765\u7ba1\u7406\u5bf9\u8c61\u5f15\u7528\uff0c\u907f\u514d\u5faa\u73af\u5f15\u7528\u7684\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u6a21\u5757\u548c\u5305\u7ba1\u7406\u7684\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>\u65e0\u6cd5\u627e\u5230\u6a21\u5757\u6216\u5305\uff08\u5bfc\u5165\u8def\u5f84\u95ee\u9898\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4f7f\u7528 <code>import<\/code> \u65f6\uff0cPython \u53ef\u80fd\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u6a21\u5757\u6216\u5305\uff0c\u5c24\u5176\u662f\u5728\u865a\u62df\u73af\u5883\u6216\u5305\u8def\u5f84\u4e0d\u6b63\u786e\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u73af\u5883\u53d8\u91cf <code>PYTHONPATH<\/code> \u8bbe\u7f6e\u6b63\u786e\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\u65f6\u786e\u4fdd\u6fc0\u6d3b\u73af\u5883\uff0c\u68c0\u67e5 <code>sys.path<\/code> \u8def\u5f84\u5217\u8868\u4e2d\u7684\u6a21\u5757\u8def\u5f84\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import sys\n  print(sys.path)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <strong>\u5faa\u73af\u5bfc\u5165\uff08Circular Import\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4e24\u4e2a\u6a21\u5757\u4e92\u76f8\u5bfc\u5165\u5bf9\u65b9\u65f6\uff0c\u4f1a\u9020\u6210\u5faa\u73af\u5bfc\u5165\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u91cd\u6784\u4ee3\u7801\uff0c\u907f\u514d\u5faa\u73af\u4f9d\u8d56\u3002\u5c06\u6a21\u5757\u5bfc\u5165\u653e\u5728\u51fd\u6570\u5185\u90e8\uff0c\u6216\u91c7\u7528\u5ef6\u8fdf\u5bfc\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u5ef6\u8fdf\u5bfc\u5165\u793a\u4f8b\n  def func():\n      from moduleB import some_function\n      some_function()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.3 <strong><code>site-packages<\/code> \u4e2d\u7684\u6a21\u5757\u7248\u672c\u4e0d\u517c\u5bb9<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>pip<\/code> \u5b89\u88c5\u7684\u5e93\u53ef\u80fd\u4e0e\u5176\u4ed6\u5e93\u7248\u672c\u4e0d\u517c\u5bb9\uff0c\u5bfc\u81f4\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>pip freeze<\/code> \u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u7684\u6240\u6709\u5e93\u53ca\u5176\u7248\u672c\uff0c\u786e\u4fdd\u7248\u672c\u517c\u5bb9\uff0c\u6216\u8005\u4f7f\u7528 <code>pipenv<\/code>\u3001<code>conda<\/code> \u7b49\u5de5\u5177\u7ba1\u7406\u865a\u62df\u73af\u5883\u548c\u4f9d\u8d56\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  pip freeze &gt; requirements.txt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Python \u73af\u5883\u4e0e\u5e73\u53f0\u7279\u6709\u7684\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u5e73\u53f0\u7279\u5b9a\u7684\u8def\u5f84\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 Windows \u548c Linux \u7b49\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u8def\u5f84\u5206\u9694\u7b26\u4e0d\u540c\uff08Windows \u4f7f\u7528 <code>\\<\/code>\uff0c\u800c Linux \u4f7f\u7528 <code>\/<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>os.path<\/code> \u6a21\u5757\u6765\u6784\u5efa\u8de8\u5e73\u53f0\u7684\u8def\u5f84\uff0c\u6216\u8005\u4f7f\u7528 <code>pathlib<\/code>\uff08Python 3.4 \u53ca\u4ee5\u4e0a\uff09\u6765\u5904\u7406\u8def\u5f84\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from pathlib import Path\n  path = Path(\"folder\") \/ \"file.txt\"  # \u81ea\u52a8\u5904\u7406\u8def\u5f84\u5206\u9694\u7b26<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>Windows \u7cfb\u7edf\u4e2d\u7684\u6743\u9650\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728 Windows \u4e0a\uff0c\u67d0\u4e9b\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u8bbf\u95ee\u6743\u9650\u53ef\u80fd\u5bfc\u81f4\u64cd\u4f5c\u5931\u8d25\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u8fd0\u884c Python \u7a0b\u5e8f\uff0c\u6216\u8005\u8c03\u6574\u6587\u4ef6\/\u76ee\u5f55\u7684\u6743\u9650\uff0c\u786e\u4fdd\u7a0b\u5e8f\u6709\u9002\u5f53\u7684\u6743\u9650\u8bbf\u95ee\u6240\u9700\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong><code>asyncio<\/code> \u4e0e <code>multiprocessing<\/code> \u4e00\u8d77\u4f7f\u7528\u65f6\u7684\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>asyncio<\/code> \u662f\u57fa\u4e8e\u4e8b\u4ef6\u5faa\u73af\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\uff0c\u800c <code>multiprocessing<\/code> \u662f\u57fa\u4e8e\u591a\u8fdb\u7a0b\u7684\u5e76\u884c\u6a21\u578b\uff0c\u4e8c\u8005\u4e00\u8d77\u4f7f\u7528\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u963b\u585e\u548c\u8d44\u6e90\u7ade\u4e89\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u907f\u514d\u540c\u65f6\u4f7f\u7528 <code>asyncio<\/code> \u548c <code>multiprocessing<\/code>\uff0c\u5982\u679c\u9700\u8981\u4f7f\u7528\u591a\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528 <code>concurrent.futures<\/code> \u6a21\u5757\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u5e76\u53d1\u548c\u591a\u7ebf\u7a0b\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>\u6b7b\u9501 (Deadlock)<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u9501\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u9075\u5faa\u56fa\u5b9a\u7684\u9501\u83b7\u53d6\u987a\u5e8f\uff0c\u4f7f\u7528 <code>try<\/code>\/<code>finally<\/code> \u8bed\u53e5\u786e\u4fdd\u9501\u80fd\u88ab\u53ca\u65f6\u91ca\u653e\uff0c\u6216\u8005\u4f7f\u7528 <code>threading.RLock()<\/code> \u4ee3\u66ff\u666e\u901a\u9501\uff0c\u5141\u8bb8\u540c\u4e00\u7ebf\u7a0b\u591a\u6b21\u83b7\u53d6\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong><code>asyncio<\/code> \u7684\u4e8b\u4ef6\u5faa\u73af\u963b\u585e<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d\uff0c\u5982\u679c\u67d0\u4e9b\u540c\u6b65\u64cd\u4f5c\u6216\u963b\u585e\u64cd\u4f5c\uff08\u5982 I\/O\uff09\u51fa\u73b0\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\uff0c\u53ef\u80fd\u5bfc\u81f4\u6574\u4e2a\u4e8b\u4ef6\u5faa\u73af\u963b\u585e\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u5c06\u6240\u6709\u7684\u963b\u585e\u64cd\u4f5c\u653e\u5165\u7ebf\u7a0b\u6c60\u6216\u8fdb\u7a0b\u6c60\uff0c\u907f\u514d\u76f4\u63a5\u5728\u4e8b\u4ef6\u5faa\u73af\u4e2d\u6267\u884c\u963b\u585e\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import asyncio\n  from concurrent.futures import ThreadPoolExecutor\n\n  def blocking_io():\n      # \u6a21\u62df\u963b\u585e I\/O \u64cd\u4f5c\n      return 'result'\n\n  async def main():\n      loop = asyncio.get_event_loop()\n      with ThreadPoolExecutor() as pool:\n          result = await loop.run_in_executor(pool, blocking_io)\n          print(result)\n\n  asyncio.run(main())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u5f02\u5e38\u5904\u7406\u548c\u9519\u8bef\u6355\u83b7\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong>\u6355\u83b7\u5f02\u5e38\u7684\u7c92\u5ea6\u8fc7\u5927<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u8fc7\u4e8e\u5bbd\u6cdb\u7684 <code>except<\/code> \u8bed\u53e5\u6355\u83b7\u4e86\u6240\u6709\u5f02\u5e38\uff0c\u5bfc\u81f4\u65e0\u6cd5\u533a\u5206\u4e0d\u540c\u7684\u9519\u8bef\uff0c\u9690\u85cf\u4e86\u6f5c\u5728\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u907f\u514d\u4f7f\u7528 <code>except:<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u7cbe\u51c6\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  try:\n      # \u4ee3\u7801\u5757\n  except (ValueError, KeyError) as e:\n      # \u5904\u7406\u7279\u5b9a\u7684\u5f02\u5e38<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong>\u5ffd\u7565\u4e86\u5f02\u5e38\u540e\u7eed\u5904\u7406<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6355\u83b7\u5f02\u5e38\u540e\u6ca1\u6709\u8fdb\u884c\u9002\u5f53\u7684\u65e5\u5fd7\u8bb0\u5f55\u6216\u540e\u7eed\u64cd\u4f5c\uff0c\u5bfc\u81f4\u9519\u8bef\u672a\u88ab\u6709\u6548\u8ddf\u8e2a\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5728\u6355\u83b7\u5f02\u5e38\u540e\uff0c\u8bb0\u5f55\u65e5\u5fd7\u3001\u6267\u884c\u56de\u6eda\u64cd\u4f5c\u6216\u901a\u77e5\u76f8\u5173\u4eba\u5458\uff0c\u4ee5\u4fbf\u53ca\u65f6\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import logging\n\n  try:\n      # \u6267\u884c\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801\n  except Exception as e:\n      logging.error(f\"Error occurred: {e}\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u6570\u636e\u5904\u7406\u548c\u8ba1\u7b97\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">5.1 <strong>\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u65f6\u53ef\u80fd\u7531\u4e8e\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u9650\u5236\u5bfc\u81f4\u7cbe\u5ea6\u95ee\u9898\uff08\u4f8b\u5982\uff0c<code>0.1 + 0.2 != 0.3<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>Decimal<\/code> \u6a21\u5757\u5904\u7406\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u7b97\uff0c\u6216\u901a\u8fc7\u56db\u820d\u4e94\u5165\u6765\u907f\u514d\u7cbe\u5ea6\u8bef\u5dee\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from decimal import Decimal\n\n  result = Decimal('0.1') + Decimal('0.2')<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.2 <strong>\u5927\u6570\u636e\u96c6\u5904\u7406\u4e2d\u7684\u6027\u80fd\u74f6\u9888<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u6ea2\u51fa\u3001I\/O \u963b\u585e\u7b49\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u751f\u6210\u5668\uff08<code>yield<\/code>\uff09\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u907f\u514d\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\uff1b\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\uff08\u5982 <code>multiprocessing<\/code> \u6216 <code>joblib<\/code>\uff09\u52a0\u901f\u6570\u636e\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  def large_data_gen():\n      for i in range(1000000):\n          yield i\n\n  # \u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\n  for item in large_data_gen():\n      process(item)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.3 <strong>\u65f6\u533a\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u8de8\u65f6\u533a\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u65f6\u95f4\u504f\u5dee\u6216\u9519\u8bef\uff0c\u5c24\u5176\u662f\u5904\u7406\u4e0d\u540c\u56fd\u5bb6\/\u5730\u533a\u7684\u65f6\u95f4\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>pytz<\/code> \u6216 <code>zoneinfo<\/code>\uff08Python 3.9 \u53ca\u4ee5\u4e0a\uff09\u6765\u660e\u786e\u5904\u7406\u65f6\u533a\u8f6c\u6362\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from datetime import datetime\n  import pytz\n\n  tz = pytz.timezone('Asia\/Shanghai')\n  time_in_shanghai = datetime.now(tz)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u7f16\u7801\u548c\u89e3\u7801\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.1 <strong>UTF-8 BOM \u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cUTF-8 \u7f16\u7801\u7684\u6587\u4ef6\u53ef\u80fd\u4f1a\u5305\u542b BOM\uff08\u5b57\u8282\u987a\u5e8f\u6807\u8bb0\uff09\uff0c\u5bfc\u81f4\u8bfb\u53d6\u6587\u4ef6\u65f6\u51fa\u73b0\u4e71\u7801\u6216\u89e3\u6790\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u6253\u5f00\u6587\u4ef6\u65f6\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\uff0c\u4f7f\u7528 <code>utf-8-sig<\/code> \u7f16\u7801\u6765\u5ffd\u7565 BOM\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open('file.txt', 'r', encoding='utf-8-sig') as f:\n      content = f.read()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">6.2 <strong>\u5b57\u7b26\u96c6\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u4e71\u7801<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5904\u7406\u4e0d\u540c\u5b57\u7b26\u96c6\uff08\u5982 <code>GBK<\/code>\u3001<code>UTF-8<\/code>\uff09\u7684\u6587\u4ef6\u6216\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u51fa\u73b0\u4e71\u7801\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u660e\u786e\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u6216\u8005\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u5c1d\u8bd5\u4f7f\u7528\u591a\u79cd\u7f16\u7801\u683c\u5f0f\u8fdb\u884c\u89e3\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\uff01\u9664\u4e86\u524d\u9762\u63d0\u5230\u7684\u5e38\u89c1\u548c\u4e0d\u5e38\u89c1\u95ee\u9898\uff0cPython \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u8fd8\u53ef\u80fd\u9047\u5230\u4e00\u4e9b\u66f4\u4e3a\u590d\u6742\u7684\u96be\u9898\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u5e76\u53d1\u3001\u5927\u6570\u636e\u5904\u7406\u3001\u7cfb\u7edf\u7ea7\u7f16\u7a0b\u7b49\u573a\u666f\u4e0b\u3002\u8fd9\u91cc\u5c06\u7ee7\u7eed\u5217\u51fa\u4e00\u4e9b\u8f83\u5c11\u9047\u5230\u4f46\u53ef\u80fd\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684 Python \u95ee\u9898\u548c\u76f8\u5173\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u9ad8\u7ea7\u5e76\u53d1\u548c\u5f02\u6b65\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">7.1 <strong>\u5f02\u6b65\u7a0b\u5e8f\u4e2d\u7684\u6b7b\u9501\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f7f\u7528 <code>asyncio<\/code> \u65f6\uff0c\u5982\u679c\u7a0b\u5e8f\u8bbe\u8ba1\u4e0d\u5f53\uff0c\u5f02\u6b65\u4efb\u52a1\u53ef\u80fd\u4f1a\u56e0\u4e3a\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u800c\u8fdb\u5165\u6b7b\u9501\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u907f\u514d\u963b\u585e\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u540c\u6b65 I\/O \u64cd\u4f5c\u653e\u5728\u5f02\u6b65\u4efb\u52a1\u4e4b\u5916\u3002\u4f7f\u7528 <code>asyncio<\/code> \u7684 <code>wait<\/code> \u6216 <code>gather<\/code> \u65b9\u6cd5\u65f6\uff0c\u8981\u786e\u4fdd\u4efb\u52a1\u7684\u987a\u5e8f\u548c\u4f9d\u8d56\u5173\u7cfb\u5408\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import asyncio\n\n  async def task1():\n      await asyncio.sleep(1)\n      return \"Task 1 complete\"\n\n  async def task2():\n      await asyncio.sleep(2)\n      return \"Task 2 complete\"\n\n  async def main():\n      result1, result2 = await asyncio.gather(task1(), task2())\n      print(result1, result2)\n\n  asyncio.run(main())<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7.2 <strong><code>asyncio<\/code> \u4e0e\u591a\u7ebf\u7a0b\u51b2\u7a81<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c <code>asyncio<\/code> \u65f6\uff0c\u7ebf\u7a0b\u548c\u4e8b\u4ef6\u5faa\u73af\u4e4b\u95f4\u7684\u4ea4\u4e92\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u7a33\u5b9a\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u907f\u514d\u5728 <code>asyncio<\/code> \u7684\u4e8b\u4ef6\u5faa\u73af\u4e2d\u76f4\u63a5\u8c03\u7528\u591a\u7ebf\u7a0b\u4ee3\u7801\u3002\u53ef\u4ee5\u4f7f\u7528 <code>loop.run_in_executor<\/code> \u5c06\u963b\u585e\u4efb\u52a1\u653e\u5165\u7ebf\u7a0b\u6c60\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import asyncio\n  from concurrent.futures import ThreadPoolExecutor\n\n  def blocking_task():\n      return \"Blocked task complete\"\n\n  async def main():\n      loop = asyncio.get_event_loop()\n      with ThreadPoolExecutor() as pool:\n          result = await loop.run_in_executor(pool, blocking_task)\n          print(result)\n\n  asyncio.run(main())<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u7cfb\u7edf\u548c\u5e95\u5c42\u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">8.1 <strong>\u6587\u4ef6\u53e5\u67c4\u6cc4\u6f0f<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6253\u5f00\u6587\u4ef6\u6216\u7f51\u7edc\u8fde\u63a5\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u663e\u5f0f\u5173\u95ed\u6587\u4ef6\u53e5\u67c4\u6216\u5957\u63a5\u5b57\uff0c\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\uff0c\u907f\u514d\u663e\u5f0f\u6253\u5f00\u540e\u5fd8\u8bb0\u5173\u95ed\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open('example.txt', 'r') as f:\n      content = f.read()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u5957\u63a5\u5b57\u7b49\u5176\u4ed6\u8d44\u6e90\uff0c\u4e5f\u5efa\u8bae\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\uff0c\u6216\u8005\u786e\u4fdd\u663e\u5f0f\u5173\u95ed\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.2 <strong><code>os.fork()<\/code> \u5f15\u8d77\u7684\u8d44\u6e90\u590d\u5236\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>os.fork()<\/code> \u65f6\uff0c\u5b50\u8fdb\u7a0b\u4f1a\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8d44\u6e90\u4e0d\u4e00\u81f4\u6216\u7ade\u6001\u6761\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u8c28\u614e\u4f7f\u7528 <code>fork()<\/code>\uff0c\u5728 Python \u4e2d\u66f4\u63a8\u8350\u4f7f\u7528 <code>multiprocessing<\/code> \u6a21\u5757\u6765\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u63a7\u5236\u548c\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">8.3 <strong>Python \u4e0e C \u6269\u5c55\u7684\u96c6\u6210\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u4f7f\u7528\u4e86 Python \u7684 C \u6269\u5c55\u6a21\u5757\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u7ba1\u7406\u3001\u5f15\u7528\u8ba1\u6570\u3001\u7ebf\u7a0b\u5b89\u5168\u7b49\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4e86\u89e3 Python \u548c C \u4e4b\u95f4\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\uff0c\u786e\u4fdd\u5728 C \u6269\u5c55\u4e2d\u6b63\u786e\u4f7f\u7528 <code>Py_INCREF<\/code> \u548c <code>Py_DECREF<\/code>\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u8c03\u8bd5\u548c\u6027\u80fd\u4f18\u5316<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">9.1 <strong>\u65e0\u6cd5\u5728 Cython \u4e2d\u8c03\u8bd5 Python \u4ee3\u7801<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 Cython \u7f16\u8bd1 Python \u4ee3\u7801\u65f6\uff0c\u53ef\u80fd\u4f1a\u4e22\u5931\u8c03\u8bd5\u4fe1\u606f\uff0c\u5bfc\u81f4\u8c03\u8bd5\u56f0\u96be\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>cython -g<\/code> \u7f16\u8bd1 Cython \u6587\u4ef6\uff0c\u8fd9\u5c06\u751f\u6210\u8c03\u8bd5\u4fe1\u606f\uff0c\u53ef\u4ee5\u5728 GDB \u6216\u5176\u4ed6\u8c03\u8bd5\u5de5\u5177\u4e2d\u8c03\u8bd5 Cython \u4ee3\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">9.2 <strong><code>numpy<\/code> \u6570\u7ec4\u8fc7\u5927\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5904\u7406\u5927\u89c4\u6a21\u7684 NumPy \u6570\u7ec4\u65f6\uff0c\u53ef\u80fd\u4f1a\u56e0\u6570\u7ec4\u5360\u7528\u8fc7\u591a\u5185\u5b58\u800c\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>dtype<\/code> \u53c2\u6570\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\uff0c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08<code>memmap<\/code>\uff09\u6765\u5904\u7406\u8d85\u5927\u6570\u7ec4\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import numpy as np\n  arr = np.memmap('large_file.dat', dtype='float32', mode='r', shape=(1000000, 100))<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">9.3 <strong>\u8c03\u8bd5 C \u6269\u5c55\u4e2d\u7684 Python \u4ee3\u7801<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f7f\u7528 C \u6269\u5c55\u65f6\uff0c\u53ef\u80fd\u51fa\u73b0\u8c03\u8bd5\u4fe1\u606f\u4e22\u5931\u6216\u8005\u8c03\u8bd5\u4e0d\u4fbf\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>gdb<\/code> \u7b49\u5de5\u5177\u8c03\u8bd5 C \u4ee3\u7801\uff0c\u5e76\u786e\u4fdd Cython \u7f16\u8bd1\u65f6\u5f00\u542f\u8c03\u8bd5\u4fe1\u606f\u3002\u53ef\u4ee5\u8003\u8651\u5c06 Python \u90e8\u5206\u7528\u6807\u51c6 Python \u6a21\u5757\u5b9e\u73b0\uff0c\u907f\u514d\u590d\u6742\u7684 C \u6269\u5c55\u8c03\u8bd5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">10. <strong>\u5206\u5e03\u5f0f\u548c\u4e91\u8ba1\u7b97<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">10.1 <strong>\u8de8\u8fdb\u7a0b\/\u5206\u5e03\u5f0f\u901a\u4fe1\u4e2d\u7684\u6d88\u606f\u4e22\u5931<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u591a\u8fdb\u7a0b\u6216\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u6d88\u606f\u4f20\u9012\u53ef\u80fd\u4f1a\u7531\u4e8e\u7f51\u7edc\u95ee\u9898\u6216\u7f13\u51b2\u533a\u6ea2\u51fa\u7b49\u539f\u56e0\u4e22\u5931\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u6d88\u606f\u961f\u5217\uff08\u5982 RabbitMQ \u6216 Kafka\uff09\u6765\u4fdd\u8bc1\u6d88\u606f\u7684\u53ef\u9760\u4f20\u9012\uff0c\u6216\u4f7f\u7528\u66f4\u5065\u58ee\u7684\u534f\u8bae\u5982 gRPC\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">10.2 <strong>\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u7684\u65f6\u949f\u540c\u6b65\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u4e0d\u540c\u8282\u70b9\u7684\u65f6\u949f\u53ef\u80fd\u4e0d\u540c\u6b65\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u65f6\u95f4\u540c\u6b65\u534f\u8bae\uff08\u5982 NTP\uff09\u6765\u4fdd\u6301\u8282\u70b9\u95f4\u65f6\u949f\u4e00\u81f4\uff0c\u6216\u8005\u4f7f\u7528\u903b\u8f91\u65f6\u949f\uff08\u5982 Lamport \u65f6\u949f\uff09\u6765\u4fdd\u8bc1\u4e8b\u4ef6\u987a\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">10.3 <strong>\u5728 Docker \u4e2d\u8fd0\u884c Python \u65f6\u7684\u6027\u80fd\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06 Python \u5e94\u7528\u90e8\u7f72\u5728 Docker \u5bb9\u5668\u4e2d\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0 I\/O \u6027\u80fd\u4e0b\u964d\u3001\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u7b49\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f18\u5316\u5bb9\u5668\u5185\u7684\u8d44\u6e90\u914d\u7f6e\uff08\u5982 CPU \u548c\u5185\u5b58\u9650\u5236\uff09\uff0c\u4f7f\u7528 Docker volume \u800c\u4e0d\u662f\u7ed1\u5b9a\u6302\u8f7d\u6765\u63d0\u9ad8 I\/O \u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">11. <strong>Python \u548c Web \u7f16\u7a0b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">11.1 <strong>Django \u7684\u6027\u80fd\u74f6\u9888<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f7f\u7528 Django \u5f00\u53d1 Web \u5e94\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6570\u636e\u5e93\u67e5\u8be2\u6548\u7387\u4f4e\u3001\u7f13\u5b58\u673a\u5236\u4e0d\u5f53\u7b49\u95ee\u9898\uff0c\u5bfc\u81f4\u5e94\u7528\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 Django \u7684\u67e5\u8be2\u4f18\u5316\u5de5\u5177\uff08\u5982 <code>select_related<\/code> \u548c <code>prefetch_related<\/code>\uff09\u907f\u514d N+1 \u67e5\u8be2\uff0c\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u4f18\u5316\u5e38\u89c1\u67e5\u8be2\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  queryset = Author.objects.select_related('book').all()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">11.2 <strong>Flask \u5f02\u6b65\u8bf7\u6c42\u7684\u963b\u585e\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Flask \u662f\u540c\u6b65\u6846\u67b6\uff0c\u5904\u7406\u5e76\u53d1\u8bf7\u6c42\u65f6\u53ef\u80fd\u51fa\u73b0\u963b\u585e\uff0c\u5c24\u5176\u662f\u5728\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u8003\u8651\u4f7f\u7528\u5f02\u6b65\u6846\u67b6\u5982 FastAPI \u6216 Tornado\uff0c\u6216\u8005\u5c06 Flask \u5e94\u7528\u4e0e <code>Celery<\/code> \u7b49\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u914d\u5408\u4f7f\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">12. <strong>\u7f16\u7801\u548c\u56fd\u9645\u5316\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">12.1 <strong>Unicode \u7f16\u7801\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u5728\u4e0d\u540c\u5e73\u53f0\u6216\u4e0d\u540c Python \u7248\u672c\u4e0b\u5904\u7406 Unicode \u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e71\u7801\u6216\u7f16\u7801\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>utf-8<\/code> \u7f16\u7801\u4f5c\u4e3a\u9ed8\u8ba4\u5b57\u7b26\u7f16\u7801\uff0c\u5e76\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u90fd\u4f7f\u7528\u7edf\u4e00\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u4f7f\u7528 utf-8 \u7f16\u7801\u6253\u5f00\u6587\u4ef6\n  with open('file.txt', 'r', encoding='utf-8') as f:\n      content = f.read()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">12.2 <strong>\u65f6\u533a\u4e0e\u56fd\u9645\u5316\u65e5\u671f\u65f6\u95f4\u5904\u7406<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8de8\u65f6\u533a\u5904\u7406\u65f6\u95f4\u65f6\uff0c\u53ef\u80fd\u9047\u5230\u65f6\u95f4\u683c\u5f0f\u4e0d\u4e00\u81f4\u6216\u9519\u8bef\u89e3\u6790\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>pytz<\/code> \u6216 <code>zoneinfo<\/code> \u6a21\u5757\u5904\u7406\u65f6\u533a\uff0c\u786e\u4fdd\u65e5\u671f\u65f6\u95f4\u5728\u4e0d\u540c\u65f6\u533a\u7684\u8f6c\u6362\u6b63\u786e\u65e0\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import pytz\n  from datetime import datetime\n\n  tz = pytz.timezone('US\/Pacific')\n  utc_now = datetime.utcnow().replace(tzinfo=pytz.utc)\n  pacific_time = utc_now.astimezone(tz)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">13. <strong>\u5176\u4ed6\u4e0d\u5e38\u89c1\u4f46\u6709\u7528\u7684 Python \u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">13.1 <strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u6027\u80fd\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u5c24\u5176\u662f\u5339\u914d\u975e\u5e38\u5927\u7684\u6587\u672c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u5339\u914d\u6a21\u5f0f\u3002\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u7684\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>re.compile()<\/code> \u9884\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u9ad8\u5339\u914d\u6548\u7387\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">13.2 <strong>Python \u7248\u672c\u517c\u5bb9\u6027\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u540c\u7248\u672c\u7684 Python\uff08\u4f8b\u5982 2.x \u4e0e 3.x\uff09\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u4e0d\u517c\u5bb9\uff0c\u7279\u522b\u662f\u5b57\u7b26\u4e32\u3001\u6574\u6570\u9664\u6cd5\u7b49\u884c\u4e3a\u4e0a\u6709\u5dee\u5f02\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u4f7f\u7528 Python 3\uff0c\u5e76\u4f7f\u7528 <code>six<\/code> \u5e93\u8fdb\u884c\u7248\u672c\u517c\u5bb9\u6027\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from six import iteritems<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">13.3 <strong>\u7c7b\u7684\u591a\u91cd\u7ee7\u627f\u548c\u83f1\u5f62\u7ee7\u627f\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u91cd\u7ee7\u627f\u65f6\uff0c\u53ef\u80fd\u51fa\u73b0\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\uff0c\u5373\u4e00\u4e2a\u7c7b\u7ee7\u627f\u4e86\u591a\u4e2a\u7236\u7c7b\uff0c\u4e14\u8fd9\u4e9b\u7236\u7c7b\u6709<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5171\u540c\u7684\u7236\u7c7b\uff0c\u5bfc\u81f4\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u65f6\u51fa\u73b0\u610f\u5916\u884c\u4e3a\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>super()<\/code> \u6765\u6b63\u786e\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u5e76\u9075\u5faa C3 \u7ebf\u6027\u5316\u7b97\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u662f\u4e00\u4e9b Python \u7f16\u7a0b\u4e2d\u8f83\u4e3a\u4e0d\u5e38\u89c1\u4f46\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u548c\u89e3\u51b3\u529e\u6cd5\u3002\u867d\u7136\u8fd9\u4e9b\u95ee\u9898\u4e0d\u4f1a\u7ecf\u5e38\u51fa\u73b0\uff0c\u4f46\u4e86\u89e3\u5b83\u4eec\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5e94\u5bf9\u5404\u79cd\u5f00\u53d1\u573a\u666f\uff0c\u5c24\u5176\u662f\u5728\u590d\u6742\u7cfb\u7edf\u548c\u5927\u89c4\u6a21\u9879\u76ee\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9664\u4e86\u5e38\u89c1\u7684 Python \u7f16\u7a0b\u95ee\u9898\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u8f83\u4e3a\u4e0d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u53ef\u80fd\u662f\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u3001\u7279\u5b9a\u73af\u5883\u6216\u8005\u590d\u6742\u5e94 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-818","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"predecessor-version":[{"id":819,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/818\/revisions\/819"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}