{"id":666,"date":"2021-09-22T16:16:29","date_gmt":"2021-09-22T08:16:29","guid":{"rendered":"https:\/\/dasmz.com\/?p=666"},"modified":"2021-09-22T16:40:36","modified_gmt":"2021-09-22T08:40:36","slug":"python3-%e5%a4%9a%e7%ba%bf%e7%a8%8b-threading-2021-9-22","status":"publish","type":"post","link":"https:\/\/dasmz.com\/?p=666","title":{"rendered":"Python3 \u591a\u7ebf\u7a0b threading  2021-9-22"},"content":{"rendered":"\n<p>Python3 \u591a\u7ebf\u7a0b threading \u5907\u5fd8\u5f55<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">t.setDaemon(True)  \/\/ \u7ebf\u7a0b\u58f0\u660e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5fc5\u987b\u5728start() \u65b9\u6cd5\u8c03\u7528\u4e4b\u524d\u8bbe\u7f6e\nt.start()  \/\/ \u5f00\u59cb\u7ebf\u7a0b\u6d3b\u52a8\nt.join()  \/\/ \u5728\u5b50\u7ebf\u7a0b\u5b8c\u6210\u8fd0\u884c\u4e4b\u524d\uff0c\u8fd9\u4e2a\u5b50\u7ebf\u7a0b\u7684\u7236\u7ebf\u7a0b\u5c06\u4e00\u76f4\u88ab\u963b\u585e<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u65b9\u6cd5\u4e0e\u5c5e\u6027 \u63cf\u8ff0\ncurrent_thread() \u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b\nactive_count() \u8fd4\u56de\u5f53\u524d\u6d3b\u8dc3\u7684\u7ebf\u7a0b\u6570\uff0c1\u4e2a\u4e3b\u7ebf\u7a0b+n\u4e2a\u5b50\u7ebf\u7a0b\nget_ident() \u8fd4\u56de\u5f53\u524d\u7ebf\u7a0b\nenumerate() \u8fd4\u56de\u5f53\u524d\u6d3b\u52a8 Thread \u5bf9\u8c61\u5217\u8868\nmain_thread() \u8fd4\u56de\u4e3b Thread \u5bf9\u8c61\nsettrace(func) \u4e3a\u6240\u6709\u7ebf\u7a0b\u8bbe\u7f6e\u4e00\u4e2a trace \u51fd\u6570\nsetprofile(func) \u4e3a\u6240\u6709\u7ebf\u7a0b\u8bbe\u7f6e\u4e00\u4e2a profile \u51fd\u6570\nstack_size([size]) \u8fd4\u56de\u65b0\u521b\u5efa\u7ebf\u7a0b\u6808\u5927\u5c0f\uff1b\u6216\u4e3a\u540e\u7eed\u521b\u5efa\u7684\u7ebf\u7a0b\u8bbe\u5b9a\u6808\u5927\u5c0f\u4e3a size\nTIMEOUT_MAX Lock.acquire(), RLock.acquire(), Condition.wait() \u5141\u8bb8\u7684\u6700\u5927\u8d85\u65f6\u65f6\u95f4<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5bf9\u4e8eThread\u7c7b\uff0c\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a\n\nthreading.Thread(self, group=None, target=None, name=None,\n     args=(), kwargs=None, *, daemon=None)\n\n# \u53c2\u6570group\u662f\u9884\u7559\u7684\uff0c\u7528\u4e8e\u5c06\u6765\u6269\u5c55\uff1b\n# \u53c2\u6570target\u662f\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u5728\u7ebf\u7a0b\u542f\u52a8\u540e\u6267\u884c\uff1b\n# \u53c2\u6570name\u662f\u7ebf\u7a0b\u7684\u540d\u5b57\u3002\u9ed8\u8ba4\u503c\u4e3a\u201cThread-N\u201c\uff0cN\u662f\u4e00\u4e2a\u6570\u5b57<meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\">\uff1b\n# \u53c2\u6570args\u548ckwargs\u5206\u522b\u8868\u793a\u8c03\u7528target\u65f6\u7684\u53c2\u6570\u5217\u8868\u548c\u5173\u952e\u5b57\u53c2\u6570<meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\">\uff1b\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Thread\u7c7b\u5b9a\u4e49\u4e86\u4ee5\u4e0b\u5e38\u7528\u65b9\u6cd5\u4e0e\u5c5e\u6027\uff1a\n\n\u65b9\u6cd5\u4e0e\u5c5e\u6027 : \u8bf4\u660e\nstart() : \u542f\u52a8\u7ebf\u7a0b\uff0c\u7b49\u5f85CPU\u8c03\u5ea6\n\nrun() : \u7ebf\u7a0b\u88abcpu\u8c03\u5ea6\u540e\u81ea\u52a8\u6267\u884c\u7684\u65b9\u6cd5\n\ngetName()\u3001setName()\u548cname : \u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u540d\u79f0\n\nsetDaemon() : \u8bbe\u7f6e\u4e3a\u540e\u53f0\u7ebf\u7a0b\u6216\u524d\u53f0\u7ebf\u7a0b\uff08\u9ed8\u8ba4\u662fFalse\uff0c\u524d\u53f0\u7ebf\u7a0b\uff09\u3002\u5982\u679c\u662f\u540e\u53f0\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u540e\u53f0\u7ebf\u7a0b\u4e5f\u5728\u8fdb\u884c\uff0c\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u540e\u53f0\u7ebf\u7a0b\u4e0d\u8bba\u6210\u529f\u4e0e\u5426\uff0c\u5747\u505c\u6b62\u3002\u5982\u679c\u662f\u524d\u53f0\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u524d\u53f0\u7ebf\u7a0b\u4e5f\u5728\u8fdb\u884c\uff0c\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u7b49\u5f85\u524d\u53f0\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u540e\uff0c\u7a0b\u5e8f\u624d\u505c\u6b62\n\nident : \u83b7\u53d6\u7ebf\u7a0b\u7684\u6807\u8bc6\u7b26\u3002\u7ebf\u7a0b\u6807\u8bc6\u7b26\u662f\u4e00\u4e2a\u975e\u96f6\u6574\u6570\uff0c\u53ea\u6709\u5728\u8c03\u7528\u4e86start()\u65b9\u6cd5\u4e4b\u540e\u8be5\u5c5e\u6027\u624d\u6709\u6548\uff0c\u5426\u5219\u5b83\u53ea\u8fd4\u56deNone\n\nis_alive() : \u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u662f\u6fc0\u6d3b\u7684\uff08alive\uff09\u3002\u4ece\u8c03\u7528start()\u65b9\u6cd5\u542f\u52a8\u7ebf\u7a0b\uff0c\u5230run()\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5\u6216\u9047\u5230\u672a\u5904\u7406\u5f02\u5e38\u800c\u4e2d\u65ad\u8fd9\u6bb5\u65f6\u95f4\u5185\uff0c\u7ebf\u7a0b\u662f\u6fc0\u6d3b\u7684\n\nisDaemon()\u65b9\u6cd5\u548cdaemon\u5c5e\u6027 : \u662f\u5426\u4e3a\u5b88\u62a4\u7ebf\u7a0b\n\njoin([timeout]) : \u8c03\u7528\u8be5\u65b9\u6cd5\u5c06\u4f1a\u4f7f\u4e3b\u8c03\u7ebf\u7a0b\u5835\u585e\uff0c\u76f4\u5230\u88ab\u8c03\u7528\u7ebf\u7a0b\u8fd0\u884c\u7ed3\u675f\u6216\u8d85\u65f6\u3002\u53c2\u6570timeout\u662f\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\uff0c\u8868\u793a\u8d85\u65f6\u65f6\u95f4\uff0c\u5982\u679c\u672a\u63d0\u4f9b\u8be5\u53c2\u6570\uff0c\u90a3\u4e48\u4e3b\u8c03\u7ebf\u7a0b\u5c06\u4e00\u76f4\u5835\u585e\u5230\u88ab\u8c03\u7ebf\u7a0b\u7ed3\u675f\u3002\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Python\u5728threading\u6a21\u5757\u4e2d\u5b9a\u4e49\u4e86\u51e0\u79cd\u7ebf\u7a0b\u9501\u7c7b\uff0c\u5206\u522b\u662f\uff1a\n\n    Lock \u4e92\u65a5\u9501\n    RLock \u53ef\u91cd\u5165\u9501\n    Semaphore \u4fe1\u53f7\n    Event \u4e8b\u4ef6\n    Condition \u6761\u4ef6\n    Barrier \u963b\u788d\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"574\" height=\"446\" src=\"https:\/\/dasmz.com\/wp-content\/uploads\/2020\/12\/DONATE.jpg\" alt=\"Donate\" class=\"wp-image-18\"\/><figcaption>\u4e91\u4e5e\u8ba8<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Python3 \u591a\u7ebf\u7a0b threading \u5907\u5fd8\u5f55 t.setDaemon(True) \/\/ \u7ebf\u7a0b\u58f0\u660e\u4e3a\u5b88\u62a4\u7ebf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/dasmz.com\/index.php?rest_route=\/wp\/v2\/posts\/666"}],"collection":[{"href":"https:\/\/dasmz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dasmz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dasmz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dasmz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=666"}],"version-history":[{"count":6,"href":"https:\/\/dasmz.com\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":674,"href":"https:\/\/dasmz.com\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions\/674"}],"wp:attachment":[{"href":"https:\/\/dasmz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dasmz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dasmz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}