测试
Topic source用循环批量创建线程不知道对不对
import threading,time from urllib import request
start=time.time()
class Mythread(threading.Thread): def init(self,name): threading.Thread.init(self) self._name=name
def run(self,url='http://www.baidu.com/'):
r=request.urlopen(url)
threads=[] for i in range(100): m=Mythread(i) threads.append(m)
for t in threads: t.start()
for t in threads: t.join()
stop=time.time()
print('time:{}'.format(stop-start))
我感觉多线程应该pool里定义10个,后面range(10),这样比才能看出来差距
import threadpool,requests,time
start_time=time.time()
def run(url):
r=requests.get(url=url)
pool=threadpool.ThreadPool(10)
reqs=threadpool.makeRequests(run,['http://www.liaoxuefeng.com' for x in range(10)])
[pool.putRequest(x) for x in reqs]
pool.wait()
print("用时:{}秒".format(time.time()-start_time))
- 1
儒生脱尘
下面是我的测试,同时访问100次廖老师的博客主页(http://www.liaoxuefeng.com/ )获取源码 ,分别使用单进程,多线程,多进程和协程:
1.单进程:
结果:
2.多线程
结果:
3.多进程
结果:
4.协程(异步IO)
结果:
在请求100次的情况下,协程和10个进程,10个线程是差不多的。再多开线程和协程需要的时间更长,所以相对来说协程更优。