{"id":207068,"date":"2025-07-08T10:49:52","date_gmt":"2025-07-08T02:49:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207068\/"},"modified":"2025-07-08T10:49:52","modified_gmt":"2025-07-08T02:49:52","slug":"golang%e7%9b%b8%e5%bd%93%e4%ba%8e%e5%9c%a8python%e4%b8%ad%e5%88%9b%e5%bb%ba%e5%ad%90%e8%bf%9b%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207068\/","title":{"rendered":"Golang\u76f8\u5f53\u4e8e\u5728Python\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b"},"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\u76f8\u5f53\u4e8e\u5728Python\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 20:27:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang\u76f8\u5f53\u4e8e\u5728Python\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5c06 python \u811a\u672c\u8f6c\u6362\u4e3a golang\uff0c\u53ea\u662f\u4e3a\u4e86\u4e86\u89e3\u6027\u80fd\u5dee\u5f02\u5e76\u5e2e\u52a9\u6211\u66f4\u591a\u5730\u5b66\u4e60 golang\u3002<\/p>\n<p>\u5728 python \u4e2d\uff0c\u6211\u6709 2 \u4e2a\u811a\u672c\u3002\u4e00\u4e2a\u662f\u8fd0\u884c\u65e0\u9650\u5faa\u73af\u7684\u811a\u672c\uff0c\u5e76\u5728\u518d\u6b21\u8fd0\u884c\u4e4b\u524d\u4f11\u7720\u4e00\u5206\u949f\u3002\u8be5\u4ee3\u7801\u68c0\u67e5\u6211\u7684\u670d\u52a1\u5668\u4e0a\u7684\u7aef\u70b9\u5e76\u8bfb\u53d6\u8f93\u51fa\uff0c\u7136\u540e\u786e\u5b9a\u662f\u5426\u9700\u8981\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002\u5982\u679c\u662f\uff0c\u5b83\u4f1a\u5904\u7406\u8f93\u51fa\u5e76\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\u3002\u5b50\u8fdb\u7a0b\u662f\u53e6\u4e00\u4e2a python \u811a\u672c\uff0c\u5b83\u6267\u884c\u5927\u91cf\u8ba1\u7b97\u5e76\u521b\u5efa\u6570\u767e\u4e2a\u7ebf\u7a0b\u3002\u5728\u4efb\u4f55\u7ed9\u5b9a\u65f6\u95f4\u90fd\u53ef\u4ee5\u8fd0\u884c\u591a\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4e14\u5b83\u4eec\u5bf9\u4e8e\u4e0d\u540c\u7684\u7528\u6237\u6765\u8bf4\u90fd\u662f\u4e0d\u540c\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u6211\u5df2\u7ecf\u4ece api \u8bfb\u53d6\u4e86 golang \u4ee3\u7801\uff0c\u5e76\u4e14\u5df2\u51c6\u5907\u597d\u542f\u52a8\u65b0\u7684\u5b50\u8fdb\u7a0b\u3002\u4f46\u6211\u4e0d\u592a\u786e\u5b9a\u8be5\u600e\u4e48\u505a\u3002 <\/p>\n<p>\u6211\u77e5\u9053\u5f53\u6211\u521b\u5efa\u65b0\u7684\u5b50\u8fdb\u7a0b\uff08\u6216\u8005\u4efb\u4f55\u4e0e go \u7b49\u6548\u7684\u4e1c\u897f\uff09\u65f6\uff0c\u6211\u53ef\u4ee5\u521b\u5efa\u4e00\u5806 go \u4f8b\u7a0b\uff0c\u4f46\u5b9e\u9645\u4e0a\u6211\u53ea\u662f\u505c\u7559\u5728\u201c\u5b50\u8fdb\u7a0b\u201d\u4f4d\u4e0a\u3002<\/p>\n<p>\u6211\u5c1d\u8bd5\u8fc7\u4f7f\u7528 go \u4f8b\u7a0b\u4f5c\u4e3a\u5b50\u8fdb\u7a0b\u7684\u66ff\u4ee3\u54c1\uff0c\u4f46\u6211\u4e0d\u8ba4\u4e3a\u8fd9\u662f\u6b63\u786e\u7684\u65b9\u6cd5\uff1f <\/p>\n<p>\u6839\u636e\u53ef\u89c6\u5316\u76ee\u7684\u7684\u8981\u6c42\uff0c\u6211\u6dfb\u52a0\u4e86\u4ee3\u7801\u793a\u4f8b\u3002 <\/p>\n<p>api.py\uff1a<\/p>\n<pre>while true:\n    someparameter = 'randomidfromdatabase'\n    subprocess.popen([\"python3\", \"mycode.py\", someparameter])\n    time.sleep(60)<\/pre>\n<p>mycode.py<\/p>\n<pre>parameter = sys.argv[1]\nfor i in range(0, 100):\n    thread.append(MyClass(parameter))\n    thread.start()<\/pre>\n<p>\u6211\u57fa\u672c\u4e0a\u9700\u8981\u76f8\u5f53\u4e8e\u201csubprocess.popen\u201d\u7684 golang\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 go <code>os\/exec<\/code> \u5305\u6765\u5b9e\u73b0\u7c7b\u4f3c\u5b50\u8fdb\u7a0b\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u5b83\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8fd0\u884c <code>date<\/code> \u7a0b\u5e8f\u5e76\u62a5\u544a\u5176\u6807\u51c6\u8f93\u51fa\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"os\/exec\"\n)\n\nfunc main() {\n    out, err := exec.command(\"date\").output()\n    if err != nil {\n        log.fatal(err)\n    }\n    fmt.printf(\"the date is %s\\n\", out)\n}<\/pre>\n<p>\u4e00\u4e2a\u66f4\u6709\u8da3\u7684\u793a\u4f8b \uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4e0e\u5df2\u542f\u52a8\u8fdb\u7a0b\u7684 stdio\/stdout \u8fdb\u884c\u4ea4\u4e92\uff1a<\/p>\n<pre>package main\n\nimport \"fmt\"\nimport \"io\/ioutil\"\nimport \"os\/exec\"\n\nfunc main() {\n\n    \/\/ We'll start with a simple command that takes no\n    \/\/ arguments or input and just prints something to\n    \/\/ stdout. The `exec.Command` helper creates an object\n    \/\/ to represent this external process.\n    dateCmd := exec.Command(\"date\")\n\n    \/\/ `.Output` is another helper that handles the common\n    \/\/ case of running a command, waiting for it to finish,\n    \/\/ and collecting its output. If there were no errors,\n    \/\/ `dateOut` will hold bytes with the date info.\n    dateOut, err := dateCmd.Output()\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(\"&gt; date\")\n    fmt.Println(string(dateOut))\n\n    \/\/ Next we'll look at a slightly more involved case\n    \/\/ where we pipe data to the external process on its\n    \/\/ `stdin` and collect the results from its `stdout`.\n    grepCmd := exec.Command(\"grep\", \"hello\")\n\n    \/\/ Here we explicitly grab input\/output pipes, start\n    \/\/ the process, write some input to it, read the\n    \/\/ resulting output, and finally wait for the process\n    \/\/ to exit.\n    grepIn, _ := grepCmd.StdinPipe()\n    grepOut, _ := grepCmd.StdoutPipe()\n    grepCmd.Start()\n    grepIn.Write([]byte(\"hello grep\\ngoodbye grep\"))\n    grepIn.Close()\n    grepBytes, _ := ioutil.ReadAll(grepOut)\n    grepCmd.Wait()\n\n    \/\/ We ommited error checks in the above example, but\n    \/\/ you could use the usual `if err != nil` pattern for\n    \/\/ all of them. We also only collect the `StdoutPipe`\n    \/\/ results, but you could collect the `StderrPipe` in\n    \/\/ exactly the same way.\n    fmt.Println(\"&gt; grep hello\")\n    fmt.Println(string(grepBytes))\n\n    \/\/ Note that when spawning commands we need to\n    \/\/ provide an explicitly delineated command and\n    \/\/ argument array, vs. being able to just pass in one\n    \/\/ command-line string. If you want to spawn a full\n    \/\/ command with a string, you can use `bash`'s `-c`\n    \/\/ option:\n    lsCmd := exec.Command(\"bash\", \"-c\", \"ls -a -l -h\")\n    lsOut, err := lsCmd.Output()\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(\"&gt; ls -a -l -h\")\n    fmt.Println(string(lsOut))\n}<\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0cgoroutine \u4e0e\u5b50\u8fdb\u7a0b\u5173\u7cfb\u4e0d\u5927\u3002 goroutines \u662f\u4e00\u79cd\u5728\u5355\u4e2a go \u8fdb\u7a0b\u4e2d\u540c\u65f6\u6267\u884c\u591a\u9879\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u4e0e\u5b50\u8fdb\u7a0b\u4ea4\u4e92\u65f6\uff0cgoroutines \u901a\u5e38\u4f1a\u6d3e\u4e0a\u7528\u573a\uff0c\u56e0\u4e3a\u5b83\u4eec\u6709\u52a9\u4e8e\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\uff0c\u540c\u65f6\u8fd8\u5728\u542f\u52a8\uff08\u4e3b\uff09\u7a0b\u5e8f\u4e2d\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u4f46\u5177\u4f53\u7ec6\u8282\u53d6\u51b3\u4e8e\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aGolang\u76f8\u5f53\u4e8e\u5728Python\u4e2d\u521b\u5efa\u5b50\u8fdb\u7a0b\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207068","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207068"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207068\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}