{"id":301331,"date":"2021-01-17T12:06:41","date_gmt":"2021-01-17T04:06:41","guid":{"rendered":"http:\/\/4563.org\/?p=301331"},"modified":"2021-01-17T12:06:41","modified_gmt":"2021-01-17T04:06:41","slug":"python-%e5%bc%82%e6%ad%a5%e4%bb%bb%e5%8a%a1%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e8%bf%90%e8%a1%8c%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=301331","title":{"rendered":"Python \u5f02\u6b65\u4efb\u52a1\u5982\u4f55\u68c0\u6d4b\u8fd0\u884c\u65f6\u95f4"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u5f02\u6b65\u4efb\u52a1\u5982\u4f55\u68c0\u6d4b\u8fd0\u884c\u65f6\u95f4               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fangwenxue <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>    async def run_task(self, task_name, task_fun):         self.logger.info(f'{task_name}')         begin_time = int(time.time())         self.task_state = False         # t = threading.Thread(target=self.check_task, args=(begin_time, task_name))         # t.start()         # t.join()             try:             await getattr(self, task_fun)()             await asyncio.sleep(1)             self.logger.warning(f'{task_name} -&gt; DONE.')             self.task_state = True         except Exception as e:             self.logger.error(e)             self.task_state = True         finally:             await self.close_page()             await asyncio.sleep(1)      def check_task(self, begin_time, task_name):         while True:             if self.task_state:                 return True              now_time = int(time.time())             n = now_time - begin_time             if n &gt; 120:                 self.logger.warning(f'{task_name} runtime {n}s.')                 return False             time.sleep(1)             <\/code><\/pre>\n<ul>\n<li>\u60f3\u8981\u5b9e\u73b0\u7684\u662f task_fun \u6267\u884c\u65f6\u95f4\u8d85\u8fc7 N \u79d2\u76f4\u63a5\u9000\u51fa run_task<\/li>\n<li>\u81ea\u5df1\u60f3\u4e86\u7b28\u65b9\u6cd5\uff0c \u5199\u4e86\u4e2a\u7ebf\u7a0b\u68c0\u6d4b\u8fd0\u884c\u65f6\u95f4\uff0c\u4f46\u4e0d\u77e5\u9053\u600e\u4e48\u629b\u5f02\u5e38\u9000\u51fa task_fun<\/li>\n<li>\u6709\u6ca1\u6709\u66f4\u597d\u7684\u5b9e\u73b0\uff1f<\/li>\n<\/ul><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5035684\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ysc3839 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e0d\u4e86\u89e3 Python \u7684 async\uff0c\u5982\u679c\u662f js \u7684\u8bdd\u53ef\u4ee5\u7528 Promise.race\uff0c\u7136\u540e\u53d6\u6d88 task \u8fd0\u884c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5035685\" 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>                                                             \u60f3\u5f97\u5012\u662f\u633a\u591a\uff0c\u4e0d\u8fc7\u4f60\u6ca1\u611f\u89c9\u8fd9\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u9700\u6c42\u5417\uff1f<\/p>\n<p>https:\/\/docs.python.org\/zh-cn\/3\/library\/asyncio-task.html#timeouts<\/p>\n<p>async def main():<br \/> # Wait for at most 1 second<br \/> try:<br \/> await asyncio.wait_for(eternity(), timeout=1.0)<br \/> except asyncio.TimeoutError:<br \/> print(&#8216;timeout!&#8217;)<\/p>\n<p>asyncio.run(main())                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5035686\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : linw1995 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u4e0d\u662f\u540c\u6b65\u5835\u585e\u4f4f\u7684\u8bdd\uff0c\u7528 `asyncio.wait_for` \u5c31\u597d\u4e86\u3002\u540c\u6b65\u5835\u585e\u7684\u8bdd\uff0c\u5efa\u8bae\u4fee\u6539\u8fd9\u4e2a\u51fd\u6570\uff0c\u901a\u8fc7 `loop.run_in_executor` \u7528\u526f\u7ebf\u7a0b\u53bb\u8dd1\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u5835\u585e\u4f4f\u4e86\u3002\u540c\u65f6\u5728\u5916\u90e8\u7528 `asyncio.wait_for`\uff0c\u4e14\u901a\u8fc7\u5728 coro \u5185\u90e8\u53bb try-except \u8fd9\u4e2a\u8d85\u65f6\u800c\u629b\u51fa\u7684 `asyncio.CancelError` \uff0c\u518d\u901a\u8fc7 theading.Event \u7b49\u65b9\u5f0f\uff0c\u628a\u53d6\u6d88\u4e8b\u4ef6\u4f20\u9012\u7ed9\u526f\u7ebf\u7a0b\u7684\u51fd\u6570                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5f02\u6b65\u4efb\u52a1\u5982\u4f55\u68c0\u6d4b\u8fd0\u884c&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\/301331"}],"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=301331"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/301331\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301331"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}