{"id":422,"date":"2022-08-19T13:26:04","date_gmt":"2022-08-19T05:26:04","guid":{"rendered":"https:\/\/www.db2go.net\/?p=422"},"modified":"2022-08-19T13:26:04","modified_gmt":"2022-08-19T05:26:04","slug":"sync-pool%e7%9a%84%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.db2go.net\/?p=422","title":{"rendered":"sync.Pool\u7684\u539f\u7406"},"content":{"rendered":"<p>\u5982\u4f55\u5728\u591a\u4e2a\u534f\u7a0b\u4e4b\u95f4\u4f7f\u7528\u540c\u4e00\u4e2a Pool \u8fbe\u5230\u9ad8\u6548\u7684\u76ee\u7684\u5462\uff1f\u5b98\u65b9\u7684\u5efa\u8bae\u662f\u5c3d\u91cf\u51cf\u5c11\u7ade\u4e89\u3002\u56e0\u4e3a sync.pool \u4f1a\u4e3a\u6bcf\u4e2aP\u5206\u914d\u4e00\u4e2a\u5b50\u6c60\u3002\u5f53\u4e00\u4e2a pool \u6267\u884cGet \u6216\u8005 Put \u64cd\u4f5c\u65f6\uff0c\u4f1a\u5148\u628a\u5f53\u524d\u7684goroutine \u56fa\u5b9a\u5230\u67d0\u4e2aP\u7684\u5b50\u6c60\u4e0a\u9762\uff0c\u7136\u540e\u518d\u5bf9\u8be5\u5b50\u6c60\u8fdb\u884c\u64cd\u4f5c\u3002\u6bcf\u4e2a\u5b50\u6c60\u91cc\u9762\u6709\u4e00\u4e2a\u79c1\u6709\u5bf9\u8c61\u548c\u5171\u4eab\u5217\u8868\u5bf9\u8c61\uff0c\u79c1\u6709\u5bf9\u8c61\u662f\u53ea\u6709\u5bf9\u5e94\u7684 P \u80fd\u591f\u8bbf\u95ee\uff0c\u56e0\u4e3a\u4e00\u4e2a P \u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6267\u884c\u4e00\u4e2a\u534f\u7a0b\uff0c\u56e0\u6b64\u5bf9\u79c1\u6709\u5bf9\u8c61\u5b58\u53d6\u64cd\u4f5c\u662f\u4e0d\u9700\u8981\u52a0\u9501\u7684\u3002\u5171\u4eab\u6c60\u662f\u548c\u5176\u4ed6P\u5206\u4eab\u7684\uff0c\u56e0\u6b64\u64cd\u4f5c\u5171\u4eab\u6c60\u662f\u9700\u8981\u52a0\u9501\u7684\u3002<\/p>\n<blockquote><p>\n  \u5bf9\u4e8e\u79c1\u6709\u5bf9\u8c61\u6765\u8bf4\uff0c\u662f\u534f\u7a0b\u5b89\u5168\u7684\uff1b\u800c\u5171\u4eab\u6c60\u662f\u534f\u7a0b\u4e0d\u5b89\u5168\u7684\u3002\n<\/p><\/blockquote>\n<p>\u5bf9\u4e8esync.Pool\u4e2d\u7684\u5bf9\u8c61\u62ff\u53d6\u7684\u6b65\u9aa4\u662f\uff1a<br \/>\n\uff081\uff09\u56fa\u5b9a\u5230\u67d0\u4e2aP\uff0c\u5c1d\u8bd5\u4ece\u79c1\u6709\u5bf9\u8c61\u83b7\u53d6\uff08\u79c1\u6709\u5bf9\u8c61\u662f\u534f\u7a0b\u5b89\u5168\u7684\uff09\u3002<br \/>\n\uff082\uff09\u5982\u679c\u79c1\u6709\u5bf9\u8c61\u975e\u7a7a\u5219\u8fd4\u56de\u8be5\u5bf9\u8c61\uff0c\u5e76\u628a\u79c1\u6709\u5bf9\u8c61\u7f6e\u7a7a\u3002<br \/>\n\uff083\uff09\u79c1\u6709\u5bf9\u8c61\u4e0d\u5b58\u5728\uff0c\u5219\u5c1d\u8bd5\u4ece\u5f53\u524dProcessor\u7684\u5171\u4eab\u6c60\u83b7\u53d6\uff08\u5171\u4eab\u6c60\u662f\u534f\u7a0b\u4e0d\u5b89\u5168\uff0c\u9700\u8981\u9501\uff09\u3002<br \/>\n\uff084\uff09\u5982\u679c\u5f53\u524dProcessor\u5171\u4eab\u6c60\u662f\u7a7a\u7684\uff0c\u90a3\u4e48\u5c1d\u8bd5\u53bb\u5176\u4ed6Processor\u7684\u5171\u4eab\u6c60\u83b7\u53d6\uff08\u4e5f\u9700\u8981\u52a0\u9501\uff09\u3002<br \/>\n\uff085\uff09\u5982\u679c\u6240\u6709\u5b50\u6c60\u90fd\u662f\u7a7a\u7684\uff0c\u6700\u540e\u4f7f\u7528\u7528\u6237\u6307\u5b9a\u7684<code>New()<\/code>\u51fd\u6570\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u8fd4\u56de\u3002<\/p>\n<p>\u5bf9\u4e8esync.Pool\u4e2d\u7684\u5bf9\u8c61\u7684\u653e\u56de\u6b65\u9aa4\u662f\uff1a<br \/>\n\uff081\uff09\u56fa\u5b9a\u5230\u67d0\u4e2aP\uff0c\u5982\u679c\u79c1\u6709\u5bf9\u8c61\u4e0d\u5b58\u5728\u5219\u4fdd\u5b58\u4e3a\u79c1\u6709\u5bf9\u8c61\u3002<br \/>\n\uff082\uff09\u5982\u679c\u79c1\u6709\u5bf9\u8c61\u5b58\u5728 \u653e\u5165\u5f53\u524dProcessor\u5b50\u6c60\u7684\u5171\u4eab\u6c60\u4e2d\uff08\u9700\u8981\u52a0\u9501\uff09\u3002<\/p>\n<blockquote><p>\n  \u4e00\u6b21 Get \u64cd\u4f5c\u6700\u5c11 0 \u6b21\u52a0\u9501\uff0c\u6700\u5927 N\uff08N \u7b49\u4e8e GOMAXPROCS\uff09\u6b21\u52a0\u9501\u3002\u4e00\u6b21 Put \u64cd\u4f5c\u6700\u5c11 0 \u6b21\u52a0\u9501\uff0c\u6700\u591a 1 \u6b21\u52a0\u9501\u3002\n<\/p><\/blockquote>\n<p>\u534f\u7a0b\u5177\u4f53\u4f1a\u5206\u914d\u5230\u90a3\u4e2a P \u6267\u884c\u662f Go\u8bed\u8a00\u7684\u534f\u7a0b\u8c03\u5ea6\u7cfb\u7edf\u51b3\u5b9a\u7684\u3002\u56e0\u6b64\u5728<code>MAXPROCS&gt;1<\/code>\u7684\u60c5\u51b5\u4e0b\uff0c\u5f53\u591a\u4e2a\u534f\u7a0b\u4f7f\u7528\u540c\u4e00\u4e2async.Pool\uff0c\u5404\u4e2aP\u7684\u5b50\u6c60\u4e4b\u95f4\u7f13\u5b58\u7684\u5bf9\u8c61\u662f\u5426\u5e73\u8861\u4ee5\u53ca\u5f00\u9500\u5982\u4f55\u662f\u6ca1\u529e\u6cd5\u51c6\u786e\u8861\u91cf\u7684\u3002\u4f46\u5982\u679c\u534f\u7a0b\u6570\u76ee\u548c\u7f13\u5b58\u7684\u5bf9\u8c61\u6570\u76ee\u8fdc\u8fdc\u5927\u4e8eGOMAXPROCS \u65f6\uff0c\u4ece\u6982\u7387\u4e0a\u6765\u8bf4\u5e94\u8be5\u662f\u76f8\u5bf9\u5e73\u8861\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728\u591a\u4e2a\u534f\u7a0b\u4e4b\u95f4\u4f7f\u7528\u540c\u4e00\u4e2a Pool \u8fbe\u5230\u9ad8\u6548\u7684\u76ee\u7684\u5462\uff1f\u5b98\u65b9\u7684\u5efa\u8bae\u662f\u5c3d\u91cf\u51cf\u5c11\u7ade\u4e89\u3002\u56e0\u4e3a sync.pool &hellip;<\/p>\n <a href=\"https:\/\/www.db2go.net\/?p=422\" title=\"sync.Pool\u7684\u539f\u7406\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">sync.Pool\u7684\u539f\u7406<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[12],"tags":[],"class_list":["entry","author-suredandan","post-422","post","type-post","status-publish","format-standard","category-golang"],"views":1696,"_links":{"self":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=422"}],"version-history":[{"count":1,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/422\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/www.db2go.net\/index.php?rest_route=\/wp\/v2\/posts\/422\/revisions\/424"}],"wp:attachment":[{"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.db2go.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}