我写了以下代码:
import eventlet
import requests
import redis
redis = redis.StrictRedis(host="localhost", port="6379", db=0)
proxy_1_pool = eventlet.GreenPool(40)
def fetch_items():
for _ in range(0, 400):
proxy_1_pool.spawn(fetch_listing)
proxy_1_pool.waitall()
def fetch_listing():
logger.info("START fetch: " + str(datetime.utcnow()))
url_info = redis.spop("listings_to_crawl")
content = make_request(url_info)
logger.info("END fetch: " + str(datetime.utcnow()))
if content:
do_something(content)
def make_request(url_info):
r = requests.get(url_info)
return r.content
def main():
fetch_items()
不幸的是,我发现fetch_listing正在顺序介入。
总会打印出来:
START
END
START
END
虽然我希望看到:
START
START
END
END
答案 0 :(得分:1)
发生了什么:
fetch_listing()
。并行的问题标题不会发生,忘了它。它按照订单执行,您可以在eventlet.sleep()
logger.info...START
进行验证
redis.spop
和requests.get
阻止。如何使阻止代码与eventlet合作:patching或卸载到线程池。
-import eventlet
+import eventlet ; eventlet.monkey_patch()
非常相关的问题,强烈推荐阅读: