{"id":43086,"date":"2024-12-01T09:50:25","date_gmt":"2024-12-01T01:50:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43086\/"},"modified":"2024-12-01T09:50:25","modified_gmt":"2024-12-01T01:50:25","slug":"golang-goroutine-%e5%ae%9e%e8%b7%b5%ef%bc%8c%e5%8a%9f%e8%83%bd%e8%bf%98%e6%98%af%e9%80%9a%e9%81%93%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43086\/","title":{"rendered":"golang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>golang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f<\/span><\/p>\n<h1>golang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 12:09:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300agolang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4e0d\u65ad\u5730\u4ecewebsocket\u63a5\u6536json\u6570\u636e\u5e76\u5728goroutine\u4e2d\u5904\u7406\u5b83\u4eec\uff0c\u4e0d\u77e5\u9053\u8fd9\u79cd\u5199\u6cd5\u662f\u5426\u503c\u5f97\u9f13\u52b1<\/p>\n<pre>ws.onmessage {     \/\/infinite receive message from websocket\n        go func() {   \/\/work find using this goroutine\n            defer processjson(message)\n        }()\n        \n        go processjson(message) \/\/error and program will terminated\n     }\n    \n    func processjson(msg string) {\n        \/\/code for  process json \n    \n        insertdatabase(processedmsg)\n    }\n    \n    func insertdatabase(processedmsg string) {\n        \/\/code insert to database\n    }<\/pre>\n<p>\u4e0b\u9762\uff08\u7b2c\u4e00\u4e2a goroutine\uff09\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u4f46\u6709\u65f6\uff08\u4e00\u5468\uff09\u8868\u660e\u4ee3\u7801\u4e2d\u5b58\u5728\u6570\u636e\u7ade\u4e89\u5e76\u7ec8\u6b62\u7a0b\u5e8f\u3002<\/p>\n<pre>go func() {  \n        defer processjson(message)\n    }()<\/pre>\n<p>\u7b2c\u4e8c\u4e2agoroutine\uff0c\u7ecf\u5e38\u5728\u8fd0\u884c\u51e0\u5206\u949f\u540e\u9047\u5230\u9519\u8bef\uff0c\u9519\u8bef\u901a\u5e38\u662f\u201c\u81f4\u547d\u9519\u8bef\uff1a\u8fd0\u884c\u65f6\u6267\u884c\u671f\u95f4\u51fa\u73b0\u610f\u5916\u4fe1\u53f7\u201d\u3002<\/p>\n<pre>go processjson(message)<\/pre>\n<p>\u636e\u6211\u4e86\u89e3\uff0c\u4e24\u4e2a goroutine \u90fd\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4e3a\u4ec0\u4e48\u7b2c\u4e00\u4e2a\u53ef\u4ee5\u8fd0\u884c\u5f97\u5f88\u597d\uff0c\u800c\u7b2c\u4e8c\u4e2a\u5374\u4e0d\u80fd\u3002\u6211\u5c1d\u8bd5\u8fc7\u4f7f\u7528\u901a\u9053\uff0c\u4f46\u4e0e\u7b2c\u4e00\u4e2a goroutine \u76f8\u6bd4\u6ca1\u6709\u592a\u5927\u533a\u522b\u3002<\/p>\n<pre>msgChan := make(chan string, 1000)\n    go processJson(msgChan)\n\n    for {   \/\/receive json from websocket, send to channel\n        msgChan &lt;- message\n    }\n\n    func JsonProcessor(msg chan string) {\n       for {   \/\/get data from channel, process in goroutine function\n           msgModified := &lt;-msg\n           insertDatabase(msgModified)\n       }\n    }<\/pre>\n<p>\u6709\u6ca1\u6709\u4ec0\u4e48\u9f13\u52b1\u7684\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e0d\u8fdb\u884c\u6570\u636e\u7ade\u4e89\u7684\u60c5\u51b5\u4e0b\u5b9e\u73b0\u76ee\u6807\uff0c\u6b22\u8fce\u63d0\u51fa\u5efa\u8bae\u3002 \u8d5e\u8d4f\u5e76\u611f\u8c22\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5c1d\u8bd5\u4f7f\u7528<code>sync.mutex<\/code>\u907f\u514d\u6570\u636e\u7ade\u4e89<\/p>\n<pre>mutux := sync.mutex{}\nws.onmessage {\n  processjson(message)\n}\nfunc processjson(msg string) {\n  mutux.lock()\n  \/\/ .........\n  mutux.unlock()\n}<\/pre>\n<p>\u5982\u679c\u5904\u7406\u529f\u80fd\u53ef\u4ee5\u5212\u5206\u800c\u65e0\u9700\u6570\u636e\u7ade\u901f\uff0c\u591a\u7ebf\u7a0b\u7248\u672c\u5982\u4e0b\uff1a<\/p>\n<pre>msgChan1 := make(chan string, 1000)\nmsgChan2 := make(chan string, 1000)\n\ngo func() {\n  for m := range msgChan1 {\n    \/\/ ...\n  }\n}()\ngo func() {\n  for m := range msgChan2 {\n    \/\/ ...\n  }\n}()\n\nws.onmessage {\n  msgChan1 &lt;- message\n  msgChan2 &lt;- message\n}\nws.onclose {\n  close(msgChan1)\n  close(msgChan2)\n}<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300agolang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; golang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f golang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-29 12:09:36 0\u6d4f\u89c8 \u6536\u85cf \u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300agolang goroutine \u5b9e\u8df5\uff0c\u529f\u80fd\u8fd8\u662f\u901a\u9053\uff1f\u300b\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 \u95ee\u9898\u5185\u5bb9 \u4e0d\u65ad\u5730\u4ecewebsocket\u63a5\u6536json\u6570\u636e\u5e76\u5728goroutine\u4e2d\u5904\u7406\u5b83\u4eec\uff0c\u4e0d\u77e5\u9053\u8fd9\u79cd\u5199\u6cd5\u662f\u5426\u503c\u5f97\u9f13\u52b1 ws.onmessage { \/\/infinite receive message from websocket go func() { \/\/work find using this goroutine defer processjson(message) }() go processjson(message) \/\/error and program will terminated } func processjson(msg string) { \/\/code for process [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43086","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43086"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43086\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}