Discuss / Python / 测试

测试

Topic source

儒生脱尘

#1 Created at ... [Delete] [Delete and Lock User]

下面是我的测试,同时访问100次廖老师的博客主页(http://www.liaoxuefeng.com/ )获取源码 ,分别使用单进程,多线程,多进程和协程:

1.单进程

import requests,time
start_time=time.time()
[requests.get('http://www.liaoxuefeng.com/') for x in range(100)]
print("用时:{}秒".format(time.time()-start_time))

结果:

用时:65.6658582687378秒

2.多线程

import threadpool,requests
def run(url):
    r=requests.get(url=url)
pool=threadpool.ThreadPool(10)
reqs=threadpool.makeRequests(run,['http://www.liaoxuefeng.com' for x in range(100)])
[pool.putRequest(x) for x in reqs]
pool.wait()
print("用时:{}秒".format(time.time()-start_time))

结果:

用时:3.0611751079559326秒

3.多进程

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import multiprocessing,time,requests
start_time=time.time()
def run(url):
    r=requests.get(url=url)
    #print(1)
if __name__=='__main__':
    pool=multiprocessing.Pool(10)
    [pool.apply_async(run,args=('http://www.liaoxuefeng.com',)) for x in range(100)]
    pool.close()
    pool.join()
    print("用时:{}秒".format(time.time()-start_time))

结果:

用时:3.8592071533203125秒

4.协程(异步IO)

import asyncio, aiohttp, time
start_time=time.time()
async def run(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url=url) as resp:
            pass
loop=asyncio.get_event_loop()
tasks=[asyncio.ensure_future(run('http://www.liaoxuefeng.com')) for x in range(100)]
loop.run_until_complete(asyncio.wait(tasks))
print("用时:{}秒".format(time.time()-start_time))

结果:

用时:4.072007894515991秒

在请求100次的情况下,协程和10个进程,10个线程是差不多的。再多开线程和协程需要的时间更长,所以相对来说协程更优。

用循环批量创建线程不知道对不对

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))

Funky-Stars

#3 Created at ... [Delete] [Delete and Lock User]

我感觉多线程应该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))

Funky-Stars

#4 Created at ... [Delete] [Delete and Lock User]

不对,我理解错了,应该都是100,pool里面的10是并发线程数

666

失眠888

#6 Created at ... [Delete] [Delete and Lock User]
  • 廖老师,各位,我想请教下,线程池一般如何设置?

  • 网上查的资料,有些写线程数目与CPU数量相同

  • 对这个多线程/线程池的数据限定不是很了解,求指点下。。。


  • 1

Reply