{"id":94398,"date":"2020-05-03T07:43:42","date_gmt":"2020-05-02T23:43:42","guid":{"rendered":"http:\/\/4563.org\/?p=94398"},"modified":"2020-05-03T07:43:42","modified_gmt":"2020-05-02T23:43:42","slug":"%e6%88%91%e6%80%80%e7%96%91%e8%bf%99%e6%98%af-python-for-%e7%9a%84%e4%b8%80%e4%b8%aa-bug","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=94398","title":{"rendered":"\u6211\u6000\u7591\u8fd9\u662f Python for \u7684\u4e00\u4e2a bug"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6211\u6000\u7591\u8fd9\u662f Python for \u7684\u4e00\u4e2a bug               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : smallgoogle <\/span>  <span><i><\/i> 21<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code># -*- coding: utf-8 -*-  from multiprocessing import Pool import os, time, random   def worker(msg, ip):     t_start = time.time()     print(\"%s \u5f00\u59cb\u6267\u884c, ip:%s, \u8fdb\u7a0b\u53f7\u4e3a%d\" % (msg['id'], ip['ip'], os.getpid()))     time.sleep(random.random() * 2)     # time.sleep(2)     t_stop = time.time()     print(msg['id'], \"\u6267\u884c\u5b8c\u6bd5, ip:%s, \u8017\u65f6%0.2f\" % (ip['ip'], t_stop - t_start))   def start(d):     po = Pool(10)     a_list = [         {\"ip\": \"ip - 1\"},         {\"ip\": \"ip - 2\"}     ]     d_list = d     for_times = 0     while len(d_list) &gt; 0:         print(\"\u7b2c %s \u6b21\" % (for_times))         x = 0         for i in d_list:             if x &lt; 2:                 po.apply_async(worker, (i, a_list[for_times]))                 d_list.pop(x)                 x+=1             else:                 print(\"==============\")                 break         for_times += 1     print(\"----start----\")     po.close()     po.join()     print(\"----end----\")    if __name__ == '__main__':     d = [{         \"id\": 1,         \"keyword\": \"site:www.aaa.com \u706f\",         \"site\": \"www.aaa.com\"     }, {         \"id\": 2,         \"keyword\": \"site:www.bbb.com \u706f\",         \"site\": \"www.bbb.com\"     }, {         \"id\": 3,         \"keyword\": \"site:www.ccc.com \u706f\",         \"site\": \"www.ccc.com\"     }, {         \"id\": 4,         \"keyword\": \"site:www.ddd.com \u706f\",         \"site\": \"www.ddd.com\"     }]     start(d) <\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6ca1\u95ee\u9898\u3002\u53ef\u662f\u4f1a\u62a5\u9519\uff1b\u6211 debug \u4e86\u3002\u5c31\u662f for \u91cc\u9762\u7684 if \u7684\u95ee\u9898\u3002<br \/> \u7406\u8bba\u4e0a whlie \u4e24\u6b21\u5c31\u7ed3\u675f\u4e86\u3002\u53ef\u662f\u4e3a\u5565\u8fd9\u91cc whlie \u8d70\u4e86\u4e09\u6b21\u5462\uff1f<br \/> while \u7b2c\u4e8c\u6b21\u7684\u65f6\u5019\uff0c\u4f1a\u770b\u5230 for \u91cc\u9762\u7684 if \u6ca1\u751f\u6548\u4e86\u3002<br \/> \u8fd9\u662f\u4e0d\u662f python \u7684 bug \uff1f<\/p>\n<p>\u6211\u5df2\u7ecf\u6392\u4e24\u5929\u3002\u65e0\u7ed3\u679c\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>16<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1206700\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GrayXu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u770b\u4ee3\u7801\u2026\u4f46\u89c9\u5f97\u662f python \u7684 bug \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206701\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yingo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c 0 \u6b21<br \/>\u7b2c 1 \u6b21<br \/>\u7b2c 2 \u6b21<br \/>Traceback (most recent call last):<br \/> File &#8220;D:pythondict.py&#8221;, line 61, in &lt;module&gt;<br \/> start(d)<br \/> File &#8220;D:pythondict.py&#8221;, line 29, in start<br \/> po.apply_async(worker, (i, a_list[for_times]))<br \/>IndexError: list index out of range<\/p>\n<p>for i in d_list:<br \/> &#8230;<br \/> d_list.pop()<br \/> &#8230;<\/p>\n<p>\u522b\u5199\u8fd9\u79cd\u4ee3\u7801.                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206702\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : renmu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u8981\u4e00\u8fb9\u5bf9\u5217\u8868\u4fee\u6539\u4e00\u8fb9\u8fdb\u884c\u5224\u65ad\uff0c\u4f1a\u5f15\u8d77\u5947\u602a\u7684\u95ee\u9898\u3002\u60f3\u590d\u5236\u7684\u8bdd\u53ef\u4ee5\u91c7\u7528 list.copy()\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u7b49\u4e8e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206703\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : NeinChn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             d_list.pop(x)\u662f\u548b\u60f3\u7684\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206704\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiri <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8c03\u8bd5\u770b\u4e86\u4e00\u4e0b\uff0c\u6ca1\u95ee\u9898\u554a\uff0c\u6700\u540e\u662f\u6570\u7ec4\u8d8a\u754c\u4e86\uff0c\u4e3b\u8981\u5c31\u662f\u4f60\u5728 for \u5faa\u73af\u91cc\u9762\u5f39\u51fa d_list \u4e2d\u7684\u5143\u7d20\u5bfc\u81f4\u7684\uff0c\u4f60\u81ea\u5df1\u53bb\u770b\u770b\u6bcf\u6b21\u5faa\u73af\u7684\u65f6\u5019 d_list \u53d8\u6210\u5565\u6837\u4e86\uff0ci \u662f\u5565\u5c31\u77e5\u9053\u4e86\uff0c\u8fd8\u6709\uff0c\u6700\u597d\u4e0d\u8981\u5199\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\uff0c\u51fa\u95ee\u9898\u80fd\u8ba9\u4f60\u5934\u75db\u6b7b<br \/>for i in d_list:<br \/>&#8230;&#8230;<br \/> d_list.pop(x)<br \/>&#8230;&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206705\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DGideas <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u7684\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u8d34\u4ee3\u7801\u5230\u8fd9\u91cc\u6765\u95ee\u95ee\u9898\uff0c\u6ca1\u6709\u63cf\u8ff0\u89e3\u91ca\u5668\u548c\u64cd\u4f5c\u7cfb\u7edf\u73af\u5883\uff0c\u522b\u4eba\u590d\u73b0\u4f60\u7684\u95ee\u9898\u4e5f\u4e0d\u4e00\u5b9a\u51c6\u554a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206706\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jirajine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u5728\u8fed\u4ee3\u65f6\u4fee\u6539\u4e86\u8fed\u4ee3\u5bf9\u8c61\u672c\u8eab\uff0c\u5f53\u7136\u4f1a\u4ea7\u751f\u96be\u4ee5\u9884\u6599\u7684\u884c\u4e3a\u3002<br \/>\u4e00\u4e2a\u7b80\u5355\u7684\u505a\u6cd5\u662f\u5728\u76ee\u6807\u5bf9\u8c61\u7684\u590d\u5236\u4e0a\u8fed\u4ee3\uff0c<br \/>\u5c06 for i in d_list: \u4fee\u6539\u4e3a for i in list(d_list):                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206707\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wysnylc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4eba\u7c7b\u603b\u662f\u89c9\u5f97\u673a\u5668\u6709\u95ee\u9898\u800c\u4e0d\u662f\u81ea\u5df1\u6709\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206708\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yingxiangyu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5faa\u73af\u8fc7\u7a0b\u4e2d\u4e0d\u8981\u4fee\u6539\u5faa\u73af\u7684\u5217\u8868\uff0c\u4f60 pop \u4e86\u5217\u8868\u5c31\u53d8\u4e86\uff0c\u7b2c\u4e8c\u6b21\u5faa\u73af\u5f00\u59cb\u662f 2 \u5143\u7d20\uff0cpop \u4e00\u4e2a\u53d8 1 \u5143\u7d20\uff0c\u8fd9\u4e2a for \u8fd8\u8981\u4e0d\u8981\u63a5\u7740\u6267\u884c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206709\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u6807\u9898\u8fd8\u6ca1\u770b\u6b63\u6587\u5c31\u60f3\uff0c\u662f\u4e0d\u662f\u53c8\u662f\u589e\u51cf\u5217\u8868\u7684\u95ee\u9898<br \/>\u8fdb\u6765\u4e00\u770b\u679c\u7136\u2026\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206710\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : freakxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9996\u5148\u8fd9\u6807\u9898\u5c31\u591f\u6807\u9898\u4e86<\/p>\n<p>import \u7684\u65f6\u5019\uff0c\u8fd9\u6837\u5199\u662f\u597d\u7684<br \/>import os<br \/>import random<br \/>import time<\/p>\n<p>\u7136\u540e<br \/>\u90fd\u7528\u5230 py3 \u4e86\uff0c\u591a\u7528\u7528 f\uff0c\u6216\u8005 format<\/p>\n<p>\u8981\u968f\u673a 1-2s<br \/>\u76f4\u63a5\u7528 random.uniform(0, 2)<\/p>\n<p>while \u5d4c\u5957 for \u8fd9\u79cd\u4e5f\u633a\u4e0d\u597d\u770b\u7684<\/p>\n<p>\u4e0d\u9700\u8981\u7279\u6b8a\u5224\u65ad\u7684\u8bdd while d_list \u5c31\u53ef\u4ee5\u4e86<\/p>\n<p>\u9700\u8981\u5224\u65ad\u7b2c N \u6b21\u7684\u8bdd\uff0c\u76f4\u63a5\u7528<br \/>for index, value in enumerate(d_list)<\/p>\n<p>\u4e0d\u8981\u4e00\u8fb9\u904d\u5386 list \u4e00\u822c\u4fee\u6539<\/p>\n<p>==============<\/p>\n<p>\u6240\u4ee5\u4e0d\u8981\u641e\u4e2a\u5927\u65b0\u95fb                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206711\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : superrichman <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             d_list.pop(x) \u95ee\u9898\u51fa\u5728\u8fd9\u91cc\uff0c\u4ed4\u7ec6\u7422\u78e8\u4e00\u4e0b\u4f60\u8981 pop \u51fa\u6765\u7684\u5143\u7d20\u548c\u4e0b\u6807\u4e3a x \u7684\u662f\u4e0d\u662f\u540c\u4e00\u4e2a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206712\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : smallgoogle <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @freakxx \u6211\u7684\u9700\u6c42\u5b9e\u9645\u4e0a\u7528 enumerate \u662f\u65e0\u6cd5\u89e3\u51b3\u7684\u3002 = = \u8fd8\u662f\u9700\u8981\u7528 list \u3002<\/p>\n<p>@imn1 \u6240\u4ee5\u4f60\u6709\u5565\u89e3\u51b3\u65b9\u6848\uff1f\u6211\u6bd4\u8f83\u60f3\u4f18\u96c5\u7684\u89e3\u51b3\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206713\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cassidyhere <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u961f\u5217\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206714\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ifzzzh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @smallgoogle #13 \u4f60\u90fd\u7528 x&lt;2 \u505a\u5224\u65ad\u4e86\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u62ff x \u505a\u5faa\u73af\u53d8\u91cf\u5462                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1206715\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ieric <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @freakxx \u8fd8\u6709\u591a\u4f59\u4ee3\u7801 d_list=d \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u6000\u7591\u8fd9\u662f Python for &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\/94398"}],"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=94398"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/94398\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94398"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}