{"id":171265,"date":"2020-01-31T09:13:38","date_gmt":"2020-01-31T01:13:38","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=171265"},"modified":"2020-01-20T15:14:17","modified_gmt":"2020-01-20T07:14:17","slug":"python-condition-parsing","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-condition-parsing.html","title":{"rendered":"Python\u7ebf\u7a0b\u6761\u4ef6\u53d8\u91cfCondition\u89e3\u6790"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Python\u7ebf\u7a0b\u6761\u4ef6\u53d8\u91cfCondition\u539f\u7406\u89e3\u6790,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Python\u7ebf\u7a0b\u6761\u4ef6\u53d8\u91cfCondition\u539f\u7406\u89e3\u6790,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b
\nCondition \u5bf9\u8c61\u5c31\u662f\u6761\u4ef6\u53d8\u91cf\uff0c\u5b83\u603b\u662f\u4e0e\u67d0\u79cd\u9501\u76f8\u5173\u8054\uff0c\u53ef\u4ee5\u662f\u5916\u90e8\u4f20\u5165\u7684\u9501\u6216\u662f\u7cfb\u7edf\u9ed8\u8ba4\u521b\u5efa\u7684\u9501\u3002\u5f53\u51e0\u4e2a\u6761\u4ef6\u53d8\u91cf\u5171\u4eab\u4e00\u4e2a\u9501\u65f6\uff0c\u4f60\u5c31\u5e94\u8be5\u81ea\u5df1\u4f20\u5165\u4e00\u4e2a\u9501\u3002\u8fd9\u4e2a\u9501\u4e0d\u9700\u8981\u4f60\u64cd\u5fc3\uff0cCondition \u7c7b\u4f1a\u7ba1\u7406\u5b83\u3002
\nacquire() \u548c release() \u53ef\u4ee5\u64cd\u63a7\u8fd9\u4e2a\u76f8\u5173\u8054\u7684\u9501\u3002\u5176\u4ed6\u7684\u65b9\u6cd5\u90fd\u5fc5\u987b\u5728\u8fd9\u4e2a\u9501\u88ab\u9501\u4e0a\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002wait() \u4f1a\u91ca\u653e\u8fd9\u4e2a\u9501\uff0c\u963b\u585e\u672c\u7ebf\u7a0b\u76f4\u5230\u5176\u4ed6\u7ebf\u7a0b\u901a\u8fc7 notify() \u6216 notify_all() \u6765\u5524\u9192\u5b83\u3002\u4e00\u65e6\u88ab\u5524\u9192\uff0c\u8fd9\u4e2a\u9501\u53c8\u88ab wait() \u9501\u4e0a\u3002
\n\u7ecf\u5178\u7684 consumer\/producer \u95ee\u9898\u7684\u4ee3\u7801\u793a\u4f8b\u4e3a\uff1a<\/p>\n

\r\nimport threading\r\nimport time\r\nimport logging\r\n\r\nlogging.basicConfig(level=logging.DEBUG,\r\nformat='(%(threadName)-9s) %(message)s',)\r\n\r\ndef consumer(cv):\r\nlogging.debug('Consumer thread started ...')\r\nwith cv:\r\nlogging.debug('Consumer waiting ...')\r\ncv.acquire()\r\ncv.wait()\r\nlogging.debug('Consumer consumed the resource')\r\ncv.release()\r\n\r\ndef producer(cv):\r\nlogging.debug('Producer thread started ...')\r\nwith cv:\r\ncv.acquire()\r\nlogging.debug('Making resource available')\r\nlogging.debug('Notifying to all consumers')\r\ncv.notify()\r\ncv.release()\r\n\r\nif __name__ == '__main__':\r\ncondition = threading.Condition()\r\ncs1 = threading.Thread(name='consumer1', target=consumer, args=(condition,))\r\n#cs2 = threading.Thread(name='consumer2', target=consumer, args=(condition,state))\r\npd = threading.Thread(name='producer', target=producer, args=(condition,))\r\n\r\ncs1.start()\r\ntime.sleep(2)\r\n#cs2.start()\r\n#time.sleep(2)\r\npd.start()<\/pre>\n

\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Python\u7ebf\u7a0b\u6761\u4ef6\u53d8\u91cfCondition\u539f\u7406\u89e3\u6790,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684 […]<\/p>\n","protected":false},"author":1893,"featured_media":171277,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-171265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/171265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1893"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=171265"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/171265\/revisions"}],"predecessor-version":[{"id":171280,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/171265\/revisions\/171280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/171277"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=171265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=171265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=171265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}