Jac*_*k z 12 python concurrency http urllib2
正如标题所示,我正在开发一个用python编写的网站,它会多次调用urllib2模块来读取网站.然后我用BeautifulSoup解析它们.
由于我必须阅读5-10个网站,因此页面需要一段时间才能加载.
我只是想知道是否有办法一次性阅读这些网站?或者是为了让它更快,我应该在每次阅读后关闭urllib2.urlopen,还是保持打开状态?
补充:另外,如果我只是切换到php,那么从其他网站获取和Parsi g HTML和XML文件会更快吗?我只是希望它加载更快,而不是目前需要的约20秒
Wai*_*ung 16
我正在使用像threading和的现代Python模块重写下面的Dumb Guy代码Queue.
import threading, urllib2
import Queue
urls_to_load = [
'http://stackoverflow.com/',
'http://slashdot.org/',
'http://www.archive.org/',
'http://www.yahoo.co.jp/',
]
def read_url(url, queue):
data = urllib2.urlopen(url).read()
print('Fetched %s from %s' % (len(data), url))
queue.put(data)
def fetch_parallel():
result = Queue.Queue()
threads = [threading.Thread(target=read_url, args = (url,result)) for url in urls_to_load]
for t in threads:
t.start()
for t in threads:
t.join()
return result
def fetch_sequencial():
result = Queue.Queue()
for url in urls_to_load:
read_url(url,result)
return result
Run Code Online (Sandbox Code Playgroud)
最佳时间find_sequencial()是2s.最佳时间fetch_parallel()是0.9 秒.
thread由于GIL,在Python中说没用是不正确的.这是线程在Python中有用的情况之一,因为线程在I/O上被阻塞.正如您在我的结果中所看到的,并行情况要快2倍.
编辑:请查看Wai的帖子以获取此代码的更好版本.请注意,此代码没有任何问题,它将正常工作,尽管下面的评论.
阅读网页的速度可能受到Internet连接的限制,而不是Python.
您可以使用线程一次加载它们.
import thread, time, urllib
websites = {}
def read_url(url):
websites[url] = urllib.open(url).read()
for url in urls_to_load: thread.start_new_thread(read_url, (url,))
while websites.keys() != urls_to_load: time.sleep(0.1)
# Now websites will contain the contents of all the web pages in urls_to_load
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29168 次 |
| 最近记录: |