Created
June 28, 2016 02:51
-
-
Save selfboot/fd6d7ace685853945c589285ab2d7b1f to your computer and use it in GitHub Desktop.
对于IO密集型任务来说,多线程仍然有一定的效果。因为在I/O请求的时候,当一个线程因I/O请求阻塞,可以调用另一个线程来继续发起对另一个Url的请求。当第一个线程的I/O请求得到回应时,它再次得到运行。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # @Last Modified time: 2016-06-28 10:50:58 | |
| import urllib2 | |
| import time | |
| from threading import Thread | |
| # 在大量访问url的时候,多线程很有优势,实际上也是空间换时间 | |
| URLs = ["http://www.baidu.com", | |
| "https://www.taobao.com/", | |
| "http://weibo.com/hi/", | |
| "http://www.jd.com/" | |
| ] | |
| def normal_way(): | |
| start = time.time() | |
| for url in URLs: | |
| urllib2.urlopen(url) | |
| print time.time() - start | |
| class URL(Thread): | |
| """ | |
| 定义多线程访问url的类 | |
| """ | |
| def __init__(self, url): | |
| self.url = url | |
| super(URL, self).__init__() | |
| def run(self): | |
| urllib2.urlopen(self.url) | |
| def multi_process_way(): | |
| num = len(URLs) | |
| threads = [] | |
| start = time.time() | |
| for i in xrange(num): | |
| t = URL(URLs[i]) | |
| threads.append(t) | |
| t.start() | |
| for i in xrange(num): | |
| threads[i].join() # 等线程执行完毕再执行主线程,保证时间上的统计是没问题的 | |
| print time.time() - start | |
| if __name__ == '__main__': | |
| normal_way() | |
| multi_process_way() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment