在你的下方看到我的抓取工具的藍圖。我想我可以用多線程加速它,但我不能。很多時候,當我加載一個網頁時,網絡服務器速度很慢,然後爬上另一個多線程加載速度更快的網頁會很好。但它不會更快。爲什麼?爲什麼我不能加快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