{"id":221688,"date":"2020-12-08T12:18:12","date_gmt":"2020-12-08T04:18:12","guid":{"rendered":"http:\/\/4563.org\/?p=221688"},"modified":"2020-12-08T12:18:12","modified_gmt":"2020-12-08T04:18:12","slug":"%e6%96%b0%e6%89%8b-%e5%85%b3%e4%ba%8e-python-%e5%8d%8f%e7%a8%8b%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=221688","title":{"rendered":"\u65b0\u624b \u5173\u4e8e Python \u534f\u7a0b\u7684\u4e00\u4e2a\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u65b0\u624b \u5173\u4e8e Python \u534f\u7a0b\u7684\u4e00\u4e2a\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : IVeverKB <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5927\u6982\u610f\u601d\u662f\u8fd9\u6837\u7684\uff0c\u534f\u7a0b\u91cc\u9762\u6709\u4e00\u4e2a\u65e0\u7ebf\u5faa\u73af A\uff0c\u4e3b\u8fdb\u7a0b\u4e5f\u5c31\u662f\u4e0b\u9762\u5199\u7684 print main loop \u662f\u4e2a\u65e0\u7ebf\u5faa\u73af B \u3002\u6709\u6ca1\u6709\u4ec0\u4e48\u65b9\u6cd5\u662f A \u548c B \u5faa\u73af\u53ef\u4ee5\u4ea4\u66ff\u6267\u884c\u7684\uff1f\u56e0\u4e3a\u6211\u73b0\u5728\u53d1\u73b0\u53ea\u6709\u8ba9 A \u4e00\u76f4\u8dd1\u6216\u8005 A \u505a\u5b8c\u4e86\u624d\u80fd\u505a B\uff0c\u4f46\u662f\u56e0\u4e3a\u5b83\u4eec\u4fe9\u90fd\u662f\u6b7b\u5faa\u73af\u5c31\u6ca1\u6cd5\u5b9e\u73b0\u540c\u65f6\u4ea4\u66ff\u6267\u884c\u3002<\/p>\n<p>\u6216\u8005\u662f\u4e0d\u662f\u6211\u8981\u5b9e\u73b0 A \u548c B \u4ea4\u66ff\u5faa\u73af\u53ea\u6709\u628a A \u5199\u6210\u591a\u7ebf\u7a0b\u624d\u884c?\u524d\u63d0\u662f\u6211\u4e0d\u60f3\u628a B \u5199\u8fdb\u53e6\u4e00\u4e2a\u534f\u7a0b\u6216\u7ebf\u7a0b\u91cc\uff0c\u6211\u53ea\u60f3\u8ba9\u5b83\u4fdd\u7559\u5728\u4e3b\u8fdb\u7a0b\u8fd9\u4e2a\u5730\u65b9\u3002<\/p>\n<p>\u591a\u8c22<\/p>\n<pre><code>import asyncio import time  async def my_async():  while True:       await asyncio.sleep(0.5)          print(\"in my_async loop\")   loop = asyncio.get_event_loop() loop.crate_task(my_async())  loop.run_forever() #loop.run_until_complete(my_async())  while True:  time.sleep(0.5)  print(\"in the main loop\") <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>14<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4480659\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 111111111111 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u56e0\u4e3a\u5b9e\u9645\u4e0a\u662f\u5355\u7ebf\u7a0b\u7684\uff0c\u4e0b\u9762\u7684 while \u8981\u7b49\u4e0a\u9762\u7684\u4ee3\u7801\u6267\u884c\u5b8c\u4e86\u624d\u6709\u673a\u4f1a\u6267\u884c\u3002<br \/>\u6216\u8bb8\u53ef\u4ee5\u8bd5\u8bd5 yield \u5173\u952e\u5b57\uff1f<\/p>\n<p>\u4f60\u8bf4\u7684\u201c\u4e0d\u60f3\u628a B \u5199\u8fdb\u53e6\u4e00\u4e2a\u534f\u7a0b\u6216\u7ebf\u7a0b\u91cc\uff0c\u6211\u53ea\u60f3\u8ba9\u5b83\u4fdd\u7559\u5728\u4e3b\u8fdb\u7a0b\u8fd9\u4e2a\u5730\u65b9\u201d \u4f60\u53ef\u4ee5\u8bf4\u8bf4\u5177\u4f53\u7684\u539f\u56e0\uff0c\u8fd9\u91cc\u53ea\u6709\u4e00\u4e2a\u8fdb\u7a0b\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u6ca1\u4ec0\u4e48\u4e3b\u4e0d\u4e3b\u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480660\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sujin190 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a B \u4e5f\u7528\u534f\u7a0b\u5c31\u662f\u4e86\u554a\uff0c\u534f\u7a0b\u8c03\u5ea6\u672c\u6765\u5c31\u72ec\u5360\u6574\u4e2a\u7ebf\u7a0b\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480661\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6309\u4f60\u8bf4\u7684\u6ca1\u529e\u6cd5\uff0c\u4f60\u8fd9\u4e2a\u8bbe\u8ba1\u6709\u95ee\u9898\uff0c\u8981\u4e0d\u5c31 A \u8fdb\u72ec\u7acb\u7ebf\u7a0b\uff0c\u8981\u4e0d\u5c31 ab \u90fd\u5728\u4e00\u4e2a event loop \u91cc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480662\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fasionchan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u534f\u7a0b\u9700\u8981\u7531\u4e8b\u4ef6\u5faa\u73af\u9a71\u52a8\uff0c\u5982\u679c\u4e3b\u7ebf\u7a0b\u6267\u884c\u4e86\u4e8b\u4ef6\u5faa\u73af\uff0c\u5c31\u6ca1\u6709\u673a\u4f1a\u6267\u884c\u5916\u90e8\u7684 while True\uff0c\u5982\u679c\u6267\u884c\u4e86 while True\uff0c\u5c31\u65e0\u6cd5\u6267\u884c\u4e8b\u4ef6\u5faa\u73af\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480663\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : IVeverKB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @111111111111 #1<br \/>@sujin190 #2<br \/>\u6211\u662f\u628a\u4e1a\u52a1\u9700\u6c42\u7b80\u5316\u4e86\u624d\u8fd9\u4e48\u8bf4\u7684\u3002\u73b0\u5728\u662f\u60f3\u8981\u5728 django \u542f\u52a8\u7684\u65f6\u5019\u5f00\u4e00\u4e2a\u534f\u7a0b\u6216\u7ebf\u7a0b\u540c\u65f6\u5904\u7406\u4e00\u4e0b\u5176\u4ed6\u4e8b\u60c5\uff0cdjango \u672c\u8eab\u5bf9\u6211\u6765\u8bf4\u5c31\u76f8\u5f53\u4e8e B \u5faa\u73af\uff0c\u6240\u4ee5\u6211\u5c31\u8fd9\u4e48\u95ee\u4e86\u3002\u5b9e\u9645\u4e0a\u662f\u6211\u4e0d\u4f1a\u628a B\uff0c\u4e5f\u5c31\u662f django \u672c\u8eab\u4e1a\u52a1\u5199\u6210\u4e00\u4e2a\u534f\u7a0b\u3002\u4e0d\u77e5\u9053\u8fd9\u4e48\u8bf4\u5bb9\u6613\u7406\u89e3\u4e0d\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480664\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : IVeverKB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable #3<br \/>@fasionchan #4<br \/>\u611f\u8c22\uff0c\u660e\u767d\u4e86\u534f\u7a0b\u80af\u5b9a\u505a\u4e0d\u4e86\u8fd9\u4e2a\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480665\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : NeilWang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u771f\u7684\u60f3\u4f7f\u7528\u534f\u7a0b\u8fd0\u884c\u4efb\u52a1 A \u7684\u8bdd\uff0c\u5176\u5b9e\u53ef\u4ee5\u5728 django \u542f\u52a8\u524d\u542f\u52a8\u4e00\u4e2a\u5b50\u7ebf\u7a0b\uff0c\u5728\u5b50\u7ebf\u7a0b\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u7136\u540e\u628a\u534f\u7a0b A \u5728\u8fd9\u4e2a\u4e8b\u4ef6\u5faa\u73af\u91cc\u8fd0\u884c\u3002<\/p>\n<p>&#8220;`<br \/>import asyncio<br \/>import time<br \/>import threading<\/p>\n<p>async def my_async():<br \/> while True:<br \/> await asyncio.sleep(0.5)<br \/> print(&#8220;in my_async loop&#8221;)<\/p>\n<p>_event_loop = asyncio.new_event_loop()<\/p>\n<p>def run_event_loop():<br \/> _event_loop.run_forever()<\/p>\n<p>threading.Thread(target=_event_loop.run_forever, daemon=True).start()<\/p>\n<p># \u5c06\u65b0\u7684\u4e8b\u4ef6\u5faa\u73af\u8bbe\u7f6e\u4e3a\u5f53\u524d\u7ebf\u7a0b\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u8fd9\u6837\u540e\u7eed\u5728\u5f53\u524d\u7ebf\u7a0b\u4e2d\u53ef\u4ee5\u4f7f\u7528 asyncio.get_event_loop() \u5f97\u5230 _event_loop<br \/>asyncio.set_event_loop(_event_loop)<\/p>\n<p># \u4e8b\u4ef6\u5faa\u73af\u4e0d\u5728\u5f53\u524d\u7ebf\u7a0b\uff0c\u9700\u8981\u4f7f\u7528 run_coroutine_threadsafe \u63d0\u4ea4\u534f\u7a0b\u4efb\u52a1<br \/>asyncio.run_coroutine_threadsafe(my_async(), asyncio.get_event_loop())<\/p>\n<p>while True:<br \/> time.sleep(0.5)<br \/> print(&#8220;in the main loop&#8221;)<br \/>&#8220;`<\/p>\n<p>\u8fd9\u6837\u7684\u8bdd\uff0c\u5728 django \u4e2d\u5904\u7406\u89c6\u56fe\u903b\u8f91\u65f6\u4e5f\u662f\u53ef\u4ee5\u63d0\u4ea4\u534f\u7a0b\u4efb\u52a1\u5230\u4e8b\u4ef6\u5faa\u73af\u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480666\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : IVeverKB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @NeilWang #7<br \/>\u975e\u5e38\u611f\u8c22\uff01\u63d0\u4f9b\u4e86\u65b0\u601d\u8def\uff0c\u8fd9\u4e2a\u60f3\u6cd5\u4e5f\u592a\u5999\u4e86\u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480667\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : keepeye <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             A \u4e5f\u8f6c\u6362\u6210\u5f02\u6b65\u7684 \u7136\u540e asyncio.gather(A(), B())<\/p>\n<p>A \u91cc\u9762 sleep \u6362\u6210 asyncio.sleep                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480668\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u6000\u7591\u4f60\u5e94\u5f53\u5bfb\u6c42 Celery \u4e00\u7c7b\u4efb\u52a1\u6846\u67b6<\/p>\n<p>\u987a\u4fbf\u4e00\u63d0\uff0cloop.run_forever() \u672c\u8eab\u5c31\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a while \u5faa\u73af\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480669\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sujin190 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @IVeverKB #5 \u4f60\u9700\u8981\u7684\u662f celery                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480670\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : IVeverKB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @NeilWang #7<br \/>\u80fd\u5426\u518d\u8ffd\u95ee\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u5b50\u7ebf\u7a0b\u5199\u5728 django \u9879\u76ee\u7684\u54ea\u4e2a\u6587\u4ef6\u4e2d\u6bd4\u8f83\u597d\u5462\uff0c\u5c31\u662f\u80fd\u505a\u5230\u4e0d\u5f71\u54cd django \u672c\u9879\u76ee\u7684\u8fd0\u884c\u4f46\u540c\u65f6\u8fd8\u80fd\u88ab\u987a\u5229\u6267\u884c\u5230\uff08\u4e14\u53ea\u4f1a\u88ab\u6267\u884c\u4e00\u6b21\uff0c\u56e0\u4e3a\u4e4b\u524d\u6709\u770b\u5230\u5199\u5728 urls.py \u3001views.py \u4e2d\u4f1a\u56e0\u4e3a\u90e8\u7f72\u5728 uwsgi+nginx \u800c\u4ea7\u751f\u591a\u8fdb\u7a0b\u6267\u884c\u591a\u6b21\uff09\uff1f\u76ee\u524d\u4e24\u4e2a\u5907\u9009\uff0c\u4e00\u4e2a\u662f project \u76ee\u5f55\u4e0b\u7684__init__.py \u6587\u4ef6\uff0c\u6216\u8005\u662f wsgi.py \u6587\u4ef6\uff0c\u4e0d\u77e5\u9053\u662f\u5426\u6ee1\u8db3\u6211\u7684\u9700\u6c42\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480671\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ClericPy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ea4\u66ff\u6267\u884c&#8230; \u4e24\u4e2a\u4efb\u52a1\u7684\u8bdd, \u529e\u6cd5\u633a\u591a\u7684, yield \u751f\u6210\u5668 \/ \u6216\u8005\u4e92\u76f8 yield \/ asyncio.Event, \u4f46\u662f\u5177\u4f53\u54ea\u4e2a\u80fd\u7528\u8fd8\u662f\u770b\u5177\u4f53\u573a\u666f, \u4f60\u8fd9\u4f8b\u5b50\u90fd\u6ca1\u4e3e\u5168, \u4e5f\u6ca1\u6cd5\u7ed9\u4f60\u8c03\u8bd5\u4e2a\u4ee3\u7801\u51fa\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4480672\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SaltCat <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 asyncio.Lock\uff0c\u8fd8\u6709\u5c31\u76f4\u63a5 asyncio \u6280\u672f\u6808\u4e00\u628a\u55e6\u5427\uff0casyncio \u7684\u4fb5\u5165\u6027\u611f\u89c9\u8fd8\u662f\u592a\u5f3a\u4e86\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u65b0\u624b \u5173\u4e8e Python \u534f\u7a0b\u7684\u4e00&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221688"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=221688"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221688\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=221688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=221688"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=221688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}