{"id":159634,"date":"2020-09-24T12:09:47","date_gmt":"2020-09-24T04:09:47","guid":{"rendered":"http:\/\/4563.org\/?p=159634"},"modified":"2020-09-24T12:09:47","modified_gmt":"2020-09-24T04:09:47","slug":"%e8%af%b7%e6%95%99-concurrent-futures-%e7%9a%84%e5%a4%9a%e8%bf%9b%e7%a8%8b-processpoolexecutor%ef%bc%8c-sumit-%e6%8f%90%e4%ba%a4%e4%bb%bb%e5%8a%a1%ef%bc%8c%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e6%8a%8a","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=159634","title":{"rendered":"\u8bf7\u6559 concurrent.futures \u7684\u591a\u8fdb\u7a0b ProcessPoolExecutor\uff0c sumit \u63d0\u4ea4\u4efb\u52a1\uff0c\u5982\u4f55\u6b63\u786e\u628a\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559 concurrent.futures \u7684\u591a\u8fdb\u7a0b ProcessPoolExecutor\uff0c sumit \u63d0\u4ea4\u4efb\u52a1\uff0c\u5982\u4f55\u6b63\u786e\u628a\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : pppguest3962 <\/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>\u53ea\u662f\u4e2a\u4f2a\u4ee3\u7801\u4f8b\u5b50, \u539f\u4ee3\u7801\u91cf\u5927\uff0c\u53ea\u80fd\u5f04\u4e86\u4e2a\u7ed3\u6784\u4e0a\u4e00\u81f4\u7684\u7b80\u5355\u7684\u4f8b\u5b50\uff0c<br \/> procFun \u5728\u6ca1\u5957\u5165\u5230\u591a\u8fdb\u7a0b\u7684\u65f6\u5019\uff0c\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u4e0d\u4f1a\u4e0e\u5176\u5b83\u4ec0\u4e48\u7684\u9020\u6210\u5e72\u6d89\u3002\u3002\u3002<\/p>\n<pre><code>BABTaskDict = {'TkNum':32,                          'TkString':'test String'}  def procFun(taskInfo):      taskNumber = taskInfo.get('TkNum')      taskString = taskInfo.get('TkString')      ...      ...      pass  with concurrent.futures.ProcessPoolExecutor(max_workers=3) as executor:              to_do = []             #executor.submit \u8fd4\u56de future \u5b9e\u4f8b             future = executor.submit(procFun, BABTaskDict ) # \u65b9\u5f0f 1             # \u65b9\u5f0f 2 future = executor.submit(procFun, *BABTaskDict )             # \u65b9\u5f0f 3 future = executor.submit(procFun, **BABTaskDict )             to_do.append(future) <\/code><\/pre>\n<p>\u7528\u4e86\u65b9\u5f0f 1 \u7684\u65b9\u6cd5\uff0c\u4f1a\u6709\u5982\u4e0b\u63d0\u793a\uff1a<\/p>\n<pre><code>Traceback (most recent call last):   File \"C:UsersAdministratorAppDataLocalProgramsPythonPython36libmultiprocessingqueues.py\", line 234, in _feed     obj = _ForkingPickler.dumps(obj)   File \"C:UsersAdministratorAppDataLocalProgramsPythonPython36libmultiprocessingreduction.py\", line 51, in dumps     cls(buf, protocol).dump(obj) TypeError: can't pickle _thread.lock objects <\/code><\/pre>\n<p>\u7528\u4e86\u65b9\u5f0f 2 \u7684\u65b9\u6cd5\uff0cfuture.result()\u6709\u5982\u4e0b\uff1a<\/p>\n<pre><code>  File \"C:UsersAdministratorAppDataLocalProgramsPythonPython36libconcurrentfutures_base.py\", line 432, in result     return self.__get_result()   File \"C:UsersAdministratorAppDataLocalProgramsPythonPython36libconcurrentfutures_base.py\", line 384, in __get_result     raise self._exception TypeError: procFun() takes 1 positional argument but 17 were given <\/code><\/pre>\n<p>\u7528\u4e86\u65b9\u5f0f 3 \u7684\u65b9\u6cd5\uff0c\u4e0e\u65b9\u5f0f 1 \u4e00\u6837\uff0c<br \/> \u662f\u4e0d\u662f\u59ff\u52bf\u4e0d\u5bf9\uff1f<br \/> \u6ca1\u5176\u5b83\u9009\u62e9\uff0cinfo \u5b57\u5178\u5185\u5bb9\u5fc5\u987b\u4f20\u8fdb\u53bb\uff0c\u6563\u5199\u9010\u4e2a\u53c2\u6570\uff0c\u975e\u5e38\u9ebb\u70e6\u7684\u3002\u3002\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3438269\" 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>                                                             \u65b9\u5f0f 1 \u7684\u5199\u6cd5\u6ca1\u95ee\u9898\uff0c\u62a5\u90a3\u9519\u662f\u56e0\u4e3a\u591a\u8fdb\u7a0b\u5728 windows \u4e0b\u8981\u7528 pickle \u6a21\u5757\u5c06\u73af\u5883\u7684\u6240\u6709\u5bf9\u8c61\u5e8f\u5217\u5316\uff0c\u518d copy \u4e00\u4efd\u7ed9\u5176\u4ed6\u8fdb\u7a0b\u3002\u6240\u4ee5\u662f procFun \u51fd\u6570\u91cc\u9762\u6709\u4ec0\u4e48\u5bf9\u8c61\u662f\u4e0d\u80fd pickle \u5e8f\u5217\u5316\u5427\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438270\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : VYSE <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             taskInfo \u8fd9\u4e2a dict \u91cc\u5b58\u4e86 thread lock, \u6240\u4ee5\u4e0d\u80fd\u5e8f\u5217\u5316\u4f20\u9012\u5230\u5b50\u8fdb\u7a0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438271\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : pppguest3962 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u918d\u9190\u704c\u9876\uff0c\u8c22\u8c22\u4e24\u4f4d\uff0c\u627e\u5230\u539f\u56e0\u4e86\uff0c\u591a\u7ebf\u7a0b \/\u8fdb\u7a0b\u73af\u5883\u4e0d\u540c\uff0c\u6211\u5728\u5728 taskInfo \u91cc\uff0c\u5c31\u628a queen \u961f\u5217\u7684\u5730\u5740\u4f20\u8fdb\u53bb\u4f7f\u7528\u4e86\uff0c <br \/>ltQueen = queue.LifoQueue() <br \/>taskInfo = {&#8216;ltQueen&#8217;: ltQueen} <br \/>\u5176\u5b9e\u5f88\u5e0c\u671b\u80fd\u5728\u5b50\u8fdb\u7a0b\u91cc\uff0c\u80fd\u505a put \u4efb\u52a1\u5230\u4e3b\u7ebf\u7a0b \/\u8fdb\u7a0b\u7684\u961f\u5217 ltQueen \u7684\u64cd\u4f5c\uff0c\u6211\u770b\u770b\u8bd5\u8bd5\u80fd\u4e0d\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u3002\u3002\uff0c\u5982\u679c\u4e0d\u6210\u529f\uff0c\u8fd8\u662f\u4f1a\u53d1\u5e16\u8bf7\u6559~~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559 concurrent.fut&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\/159634"}],"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=159634"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/159634\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159634"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}