概述
- 今天, 使用
python3
模擬下發包, http get
請求, 然后 采用tomorrow
多線程.
代碼如下:
# coding=utf-8
from urllib import request
import json
from tomorrow import threads
import time
@threads(100)
def testgetHtml(url):
requests = request.urlopen(url)
return requests
start = time.time()
url = "https://www.baidu.com/"
print('begin')
responses = [testgetHtml(url) for i in range(100)]
# html = [(json.loads(response.read().decode('utf-8')))['total']
html = [response.read().decode('utf-8') for response in responses]
for x in html:
print(x)
tomorrow
包准備
pip install tomorrow
運行環境遇到的問題
- 移動python目錄后, pip 默認安裝路徑與實際項目不匹配. 現象來說就是: cd 進入項目目錄后運行如上安裝命令是成功的, 但是運行python程序依然找不到
tomorrow
包, 開始懷疑默認pip路徑安裝問題. 解決方法步驟如下:
- 更改了
path
系統環境, 修改python 及 python\scripts 的路徑, 不生效, 錯誤依舊.
- 重新安裝
pip
. 方法: pip 官網下載后, 解壓.tar.gz
的格式, 然后 python setup.py install
安裝成功, 錯誤消失
其他嘗試未果
- 嘗試一個方法內完成Request 和 response 讀取顯示.: 各種報錯, 未繼續.
- 可能的原因: 涉及異步過程, 無法合理追蹤, 與預期的多線程有出入.