我有一個函數,它在單個線程中工作。這是一個簡單的例子。基本上我想驗證幾個死鏈接並將結果存儲在字典列表中的每個項目中。對函數使用多處理失敗,字符串索引必須是整數
import requests
import sys
import logging
def main():
urls_to_check = [{'url': 'http://example.com'},
{'url': 'http://example.com'},
{'url': 'http://example.com'}]
print check_for_404(urls_to_check)
def check_for_404(urls_to_check):
for item in urls_to_check:
r = requests.get(item['url'])
item.update({'responseCode': r.status_code})
return urls_to_check
if __name__ == '__main__':
try:
main()
except:
logging.error("Unexpected error:" + str(sys.exc_info()))
輸出:
[{'url': 'http://example.com', 'responseCode': 200}, {'url': 'http://example.com', 'responseCode': 200}, {'url': 'http://example.com', 'responseCode': 200}]
如果我實現了多,這是我理解的是跨多個內核和運行部分分裂的迭代,我很高興與
現在可通過函數迭代...
import requests
import sys
import logging
from multiprocessing import Pool
def main():
urls_to_check = [{'url': 'http://example.com'},
{'url': 'http://example.com'},
{'url': 'http://example.com'}]
p = Pool(5)
print p.map(check_for_404, urls_to_check)
def check_for_404(urls_to_check):
for item in urls_to_check:
r = requests.get(item['url'])
item.update({'responseCode': r.status_code})
return urls_to_check
if __name__ == '__main__':
try:
main()
except:
logging.error("Unexpected error:" + str(sys.exc_info()))
我收到錯誤TypeError('string indices must be integers, not str',), <traceback object at 0x10ad6c128>)
如何實現多處理,以便我可以更快地處理一長串url?
這是我在看教程: https://docs.python.org/2/library/multiprocessing.html
請出示完整的錯誤消息和完整回溯。 – BrenBarn
全部完成。對不起 –