search

Home  >  Q&A  >  body text

python2.7 - python关于回调函数的理解

我的代码想要通过count来计数,然后通过callback来调用tmp方法
本来想做的是每次循环,urls与topic_id一一对应,然后发出request到下一个方法中,但是,我下面的代码的结果是,先循环玩count一直到最后,然后才调用callback,并且传入的参数为topic_ids[0],是第一个,我不太理解callback,请指教~

count = 0
        # print topic_ids
        for u in urls:
            # print u
            # print topic_ids[count]
            yield scrapy.Request(
                    url=self.zhihu_topics_url + u,
                    # url = "https://m.zhihu.com/topics#物理学",
                    headers=headers,
                    meta={
                        "proxy": proxy,
                        "cookiejar": response.meta["cookiejar"],
                        "topic_id": topic_ids[count],
                    },
                    callback=self.tmp)
            count +=1
            print count

    def tmp(self, response):
        print "topic_id:  " + response.meta['topic_id']
高洛峰高洛峰2887 days ago239

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:44:58

    I don’t think it’s a problem with the callback function
    Because the program is non-blocking, so the loop must be completed first, then wait for the network io, capture the data, and then execute your callback function

    reply
    0
  • Cancelreply