{"id":125622,"date":"2020-06-24T13:38:22","date_gmt":"2020-06-24T05:38:22","guid":{"rendered":"http:\/\/4563.org\/?p=125622"},"modified":"2020-06-24T13:38:22","modified_gmt":"2020-06-24T05:38:22","slug":"python-%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b-asyncio-lock-%e5%a6%82%e4%bd%95%e8%ae%a9%e5%a4%9a%e4%b8%aa%e5%8d%8f%e7%a8%8b%e5%85%b1%e4%ba%ab%e4%b8%80%e6%8a%8a%e9%94%81","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=125622","title":{"rendered":"Python \u5f02\u6b65\u7f16\u7a0b asyncio lock \u5982\u4f55\u8ba9\u591a\u4e2a\u534f\u7a0b\u5171\u4eab\u4e00\u628a\u9501"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u5f02\u6b65\u7f16\u7a0b asyncio lock \u5982\u4f55\u8ba9\u591a\u4e2a\u534f\u7a0b\u5171\u4eab\u4e00\u628a\u9501               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CamD <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5148\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre><code>import asyncio import aioredis  # redis \u6c60 redis_pool = {  }  # redis \u6c60\u8bfb\u5199\u9501 _lock = asyncio.Lock()  async def get_db(index:str=\"0\"):     global redis_pool     await _lock.acquire() # \u8bf7\u6c42\u9501     db = redis_pool.get(index)     print(db)     if db is None:         specify_db = await aioredis.create_redis_pool(\"redis:\/\/localhost\",db=int(index))         redis_pool.update({index:specify_db})         _lock.release() # \u66f4\u65b0 redis \u6c60\u5b8c\u6bd5\uff0c\u91ca\u653e\u9501         return specify_db          _lock.release() # \u4e0d\u9700\u8981\u751f\u6210 redis \u8fde\u63a5\uff0c\u7acb\u5373\u91ca\u653e\u9501     return db   async def put_json(key:str,value:str,db_index:str=\"0\",expire=-1):     db = await get_db(db_index)     await db.set(key,value,expire=expire)      async def test():     t1 = asyncio.create_task(get_db(\"0\"))     t2 = asyncio.create_task(get_db(\"0\"))     t3 = asyncio.create_task(get_db(\"0\"))     t4 = asyncio.create_task(get_db(\"0\"))     t5 = asyncio.create_task(get_db(\"0\"))     task_list = [t1, t2, t3, t4, t5]     await asyncio.wait(task_list)       if __name__ == '__main__':     asyncio.get_event_loop().run_until_complete(test())  <\/code><\/pre>\n<p>\u8fd9\u662f\u6211\u6784\u5efa\u4e86\u4e00\u4e2a\u7b80\u6613\u7684 aioredis \u8fde\u63a5\u6c60\uff0c\u5982\u679c\u5355\u7eaf\u6267\u884c\u8fd9\u4e2a test \u7684\u65f6\u5019 lock \u53ef\u4ee5\u6b63\u786e\u4f7f\u7528\uff0c\u4f46\u4e00\u65e6\u5176\u4ed6\u5f02\u6b65\u4ee3\u7801\u901a\u8fc7 from import \u8c03\u7528<code>put_json()<\/code>\u90a3\u4e48<code>get_db()<\/code>\u5c31\u4f1a\u629b\u51fa<code>... got Future &lt;Future pending&gt; attached to a different loop<\/code>,\u8bf4<code>lock.acquire()<\/code>\u548c \u5176\u4ed6\u4ee3\u7801\u4e0d\u518d\u540c\u4e00\u4e2a\u4e8b\u52a1\u5faa\u73af\u4e2d<\/p>\n<p>\u800c\u4e14\u67e5\u9605\u6e90\u7801\u770b\u5230 <code>asyncio.Lock()<\/code>\u82e5\u4e0d\u6307\u5b9a loop \u5faa\u73af\u5c31\u4f1a\u901a\u8fc7<code>asyncio.get_event_loop()<\/code>\u5f97\u5230\u4e00\u4e2a\u65b0\u5faa\u73af\uff0c\u7f51\u4e0a\u5bf9\u4e8e asyncio.Lock \u5bf9\u8c61\u8d44\u6599\u592a\u5c11\u4e86\u3002\u53ea\u6709\u5c11\u91cf\u7684 demo\uff0cdemo \u4e2d\u662f\u5728 create_task \u4e4b\u524d\u5c31\u521b\u5efa\u597d\u9501\uff0c\u7136\u540e\u53c2\u6570\u4f20\u9012\u7ed9\u5f02\u6b65\u65b9\u6cd5\uff0c\u8fd9\u6837\u505a\u5bf9 demo \u6709\u6548\uff0c\u4f46\u5bf9\u5c42\u6b21\u592a\u591a\u7684\u4ee3\u7801\u5f88\u4e0d\u53cb\u597d<\/p>\n<p>\u6211\u73b0\u5728\u7684\u95ee\u9898\u662f\u6211\u4e0d\u60f3\u4ece create_task()\u524d\u5c31\u521b\u5efa\u4e00\u628a\u9501\u7136\u540e\u4e00\u76f4\u7528\u53c2\u6570\u4f20\u9012\u5230 get_db()\u4e2d\uff0c\u56e0\u4e3a\u8fd9\u628a\u9501\u53ea\u9488\u5bf9 redis_pool \u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u5176\u4ed6\u4ee3\u7801\u6839\u672c\u4e0d\u9700\u8981\u8fd9\u628a\u9501\uff0c\u8fd8\u6709\u6709\u4ec0\u4e48\u5176\u4ed6\u7684\u529e\u6cd5\u53ef\u4ee5\u8c03\u5ea6\u534f\u7a0b\u5bf9\u67d0\u4e2a\u5bf9\u8c61\u7684\u8bfb\u5199\u5417<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2155316\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yzk66880 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8211; \u4f60\u89c9\u5f97 create_task() \u540e\u5207\u6362\u65f6\u95f4\u5faa\u73af\u4e86\u5417\uff1f<br \/>&#8211; \u4e3a\u4ec0\u4e48 aioredis \u7684\u8bfb\u5199\u9700\u8981\u9501\uff1f\u4f60\u8ba4\u4e3a\u4ed6\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u5417\uff1f<br \/>&#8211; aioredis \u597d\u50cf\u6709\u5b9e\u73b0\u8fde\u63a5\u6c60&#8230;.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5f02\u6b65\u7f16\u7a0b async&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\/125622"}],"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=125622"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/125622\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=125622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=125622"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=125622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}