{"id":418632,"date":"2021-03-12T00:39:55","date_gmt":"2021-03-11T16:39:55","guid":{"rendered":"http:\/\/4563.org\/?p=418632"},"modified":"2021-03-12T00:39:55","modified_gmt":"2021-03-11T16:39:55","slug":"%e8%af%b7%e6%95%99%e6%9c%89%e5%85%b3-python-%e5%a4%9a%e8%bf%9b%e7%a8%8b-multiprocessing-%e4%b8%ad%e4%bd%bf%e7%94%a8-queue-%e4%ba%a7%e7%94%9f%e7%9a%84%e6%ad%bb%e9%94%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=418632","title":{"rendered":"\u8bf7\u6559\u6709\u5173 Python \u591a\u8fdb\u7a0b multiprocessing \u4e2d\u4f7f\u7528 Queue \u4ea7\u751f\u7684\u6b7b\u9501\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u6709\u5173 Python \u591a\u8fdb\u7a0b multiprocessing \u4e2d\u4f7f\u7528 Queue \u4ea7\u751f\u7684\u6b7b\u9501\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mrning <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u60f3\u5b9e\u73b0\u591a\u8fdb\u7a0b\u6267\u884c\u67d0\u9879\u4efb\u52a1\uff08 moviepy \u591a\u8fdb\u7a0b\uff09\uff0c\u83b7\u5f97\u8fd4\u56de\u7684\u503c\uff08 frame\uff0cnumpy \u6570\u7ec4\uff09\u3002 \u5c1d\u8bd5\u8fc7\u7528 ApplyResult.get() \u83b7\u53d6\uff0c\u4e0d\u8fc7\u8fd4\u56de\u7684\u5185\u5bb9\u4e0d\u80fd\u88ab pickle\uff0c\u6240\u4ee5\u7528\u4e86 queue \u3002 \u6216\u8005\u8fd8\u6709\u5176\u4ed6\u66f4\u597d\u7684\u65b9\u6cd5\u4e48<\/p>\n<p>\u4e0b\u9762\u662f\u4e2a\u7b80\u5355\u7684 demo\uff0c\u4f3c\u4e4e\u9047\u5230\u4e86\u6b7b\u9501\uff0c\u7f51\u4e0a\u6ca1\u6709\u627e\u5230\u597d\u7684\u65b9\u6848\uff0c\u6c42\u6559<\/p>\n<pre><code>import multiprocessing as mp from multiprocessing import Pool,Queue import os,time,random   def worker(msg,q):     t_start = time.time()     print(\"%s  Started with pid: %d\"%(msg,os.getpid()))     a = 2**100000000     t_stop = time.time()     print(msg,\"  Finishing with time: %0.2f\"%(t_stop-t_start))         q.put(a)        if __name__ == '__main__':     mp.set_start_method(\"spawn\")     pool = mp.Pool(3)      q=mp.Queue()     for i in range(10):         print(i)         pool.apply_async(worker,(i,q,))     pool.close()     for thread_i in range(10):             result = q.get()             print(result)      pool.join()     print(\"Waiting\") <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5576872\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaolinjia <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             q = mp.Manager().Queue()\u3002\u4e2a\u4eba\u7ecf\u9a8c\u662f\u5f53\u4f60\u7528 apply.async \u5361\u4f4f\u6ca1\u629b\u51fa\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u628a\u4ed6\u6539\u6210 apply \u963b\u585e\u578b\uff0c\u770b\u662f\u62a5\u5565\u9519\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5576873\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nikan999 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6253\u5370\u9519\u8bef<br \/>Queue objects should only be shared between processes through inheritance<br \/>\u53c2\u8003\uff1a<br \/>https:\/\/stackoverflow.com\/questions\/9908781\/sharing-a-result-queue-among-several-processes                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5576874\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jblue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u628a\u8fd4\u56de\u503c\u5b58\u8d77\u6765\u653e\u5230 redis \u4e4b\u7c7b\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5576875\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : mrning <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xiaolinjia <br \/>@nikan999 <br \/>\u6539\u4e86\u4e0b\uff0c\u5728 linux \u4e0b\u6267\u884c\uff0c\u80fd\u770b\u5230 print \u7684\u4fe1\u606f\u4e86\u3002worker \u51fd\u6570\u6267\u884c\u5b8c\u4e86\uff0c\u7136\u540e\u5728 result = q.get()\u8fd9\u91cc\u5361\u4f4f\u4e86<\/p>\n<p>@Jblue <br \/>\u5b9e\u5728\u4e0d\u884c\u7684\u8bdd&#8230;\u4e5f\u662f\u4e2a\u529e\u6cd5                                                             <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5576876\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : mrning <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ![py11.PNG]( https:\/\/i.loli.net\/2021\/03\/26\/qp5MS1olWGixHmA.png)<\/p>\n<p>\u4ee3\u7801\u53ca\u6267\u884c\u7ed3\u679c                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5576877\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nikan999 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             print 2**100000000<br \/>\u8fd9\u4e2a\u6570\u592a\u5927\u4e86\u5bfc\u81f4\u5361\u5728\u6253\u5370\u8fd9\u4e00\u5757                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5576878\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : mrning <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nikan999 \u539f\u6765\u5982\u6b64                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5576879\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hareandlion <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             queue \u7528 mp.Manager().Queue()\uff0c\u8f93\u51fa\u8d85\u7ea7\u5927\u6570\u8ba9\u6211\u673a\u5668\u5185\u5b58\u65e0\u6cd5\u91ca\u653e\u2026\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u6709\u5173 Python \u591a\u8fdb\u7a0b m&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/418632"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=418632"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/418632\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=418632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=418632"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=418632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}