{"id":21,"date":"2021-09-09T10:52:58","date_gmt":"2021-09-09T02:52:58","guid":{"rendered":"http:\/\/139.155.46.233\/?p=21"},"modified":"2021-09-09T10:52:58","modified_gmt":"2021-09-09T02:52:58","slug":"python%e5%a4%9a%e7%ba%bf%e7%a8%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.lucas88.cn\/?p=21","title":{"rendered":"Python\u591a\u7ebf\u7a0b\u8be6\u89e3"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ==============Code==============; notranslate\" title=\"==============Code==============\">\n\n# encoding: UTF-8\nimport thread\nimport time\n\n# \u4e00\u4e2a\u7528\u4e8e\u5728\u7ebf\u7a0b\u4e2d\u6267\u884c\u7684\u51fd\u6570\ndef func():\nfor i in range(5):\nprint 'func'\ntime.sleep(1)\n\n# \u7ed3\u675f\u5f53\u524d\u7ebf\u7a0b\n# \u8fd9\u4e2a\u65b9\u6cd5\u4e0ethread.exit_thread()\u7b49\u4ef7\nthread.exit() # \u5f53func\u8fd4\u56de\u65f6\uff0c\u7ebf\u7a0b\u540c\u6837\u4f1a\u7ed3\u675f\n\n# \u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u7ebf\u7a0b\u7acb\u5373\u5f00\u59cb\u8fd0\u884c\n# \u8fd9\u4e2a\u65b9\u6cd5\u4e0ethread.start_new_thread()\u7b49\u4ef7\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u65b9\u6cd5\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u65b9\u6cd5\u7684\u53c2\u6570\nthread.start_new(func, ()) # \u65b9\u6cd5\u6ca1\u6709\u53c2\u6570\u65f6\u9700\u8981\u4f20\u5165\u7a7atuple\n\n# \u521b\u5efa\u4e00\u4e2a\u9501\uff08LockType\uff0c\u4e0d\u80fd\u76f4\u63a5\u5b9e\u4f8b\u5316\uff09\n# \u8fd9\u4e2a\u65b9\u6cd5\u4e0ethread.allocate_lock()\u7b49\u4ef7\nlock = thread.allocate()\n\n# \u5224\u65ad\u9501\u662f\u9501\u5b9a\u72b6\u6001\u8fd8\u662f\u91ca\u653e\u72b6\u6001\nprint lock.locked()\n\n# \u9501\u901a\u5e38\u7528\u4e8e\u63a7\u5236\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\ncount = 0\n\n# \u83b7\u5f97\u9501\uff0c\u6210\u529f\u83b7\u5f97\u9501\u5b9a\u540e\u8fd4\u56deTrue\n# \u53ef\u9009\u7684timeout\u53c2\u6570\u4e0d\u586b\u65f6\u5c06\u4e00\u76f4\u963b\u585e\u76f4\u5230\u83b7\u5f97\u9501\u5b9a\n# \u5426\u5219\u8d85\u65f6\u540e\u5c06\u8fd4\u56deFalse\nif lock.acquire():\ncount += 1\n\n# \u91ca\u653e\u9501\nlock.release()\n\n# thread\u6a21\u5757\u63d0\u4f9b\u7684\u7ebf\u7a0b\u90fd\u5c06\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u540e\u540c\u65f6\u7ed3\u675f\ntime.sleep(6)\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p># encoding: UTF-8 import thread import time # \u4e00\u4e2a\u7528\u4e8e\u5728\u7ebf\u7a0b\u4e2d\u6267&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/www.lucas88.cn\/?p=21\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":1,"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":22,"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions\/22"}],"wp:attachment":[{"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lucas88.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}