在你的下方看到我的抓取工具的蓝图。我想我可以用多线程加速它,但我不能。很多时候,当我加载一个网页时,网络服务器速度很慢,然后爬上另一个多线程加载速度更快的网页会很好。但它不会更快。为什么?为什么我不能加快python中的多线程爬行?
def start_it():
while(True):
get_urls()
def get_urls():
response = urllib2.urlopen(url)
page_source = str(response.read())
pool = ThreadPool(10)
pool.map(start_it())
好吧我测试过,如果线程并行运行,它们不是:/我在做什么错了?
def start_it():
x = random.random()
while(True):
get_urls(x)
def get_urls(x):
print(x)
pool = ThreadPool(10)
pool.map(start_it())
我知道这是因为输出始终是相同的:
0.1771815430790964
0.1771815430790964
0.1771815430790964
0.1771815430790964
0.1771815430790964
0.1771815430790964
0.1771815430790964
你确定你的线程实际上是同时运行吗?把一些日志记录放进去。你知道GIL及其对线程代码的影响吗?微型加工可能会更好。 – scytale
多处理有什么区别? –
请详细阅读我以前的评论 – scytale