关于文章 aiohttp

ab aiohttp linux python python3.6 python3.7 torando uvloop

python3.7 和python3.6 压力测试 aiohttp-tornado-uvloop

aiohttp-tornado-uvloop 在python3.7下进行了压力测试

统一的ab

ab -n 10000 -c 1000 "http://0.0.0.0:8080/"

结果

aiohttp
    asyncio
        3.7 : 4000
        3.6 : 3300
    uvloop
        3.7 : 4300
        3.6 : 4700
tornado
    ioloop
        3.7 : 3100
        3.6 : 1300
    uvloop
        3.7 : 1700
        3.6 : 1700

aiohttp

from aiohttp import web

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/{name}', handle)])

web.run_app(app)

aiohttp + uvloop

from aiohttp import web
import uvloop
import asyncio
async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/{name}', handle)])
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
app._set_loop(loop)

web.run_app(app)

tornado

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8080)
    tornado.ioloop.IOLoop.current().start()

tornado + uvloop

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

import uvloop
import asyncio

from tornado.platform.asyncio import BaseAsyncIOLoop


class TornadoUvloop(BaseAsyncIOLoop):

    def initialize(self, **kwargs):
        loop = uvloop.new_event_loop()
        try:
            super(TornadoUvloop, self).initialize(
                loop, close_loop=True, **kwargs)
        except Exception:
            loop.close()
            raise

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8080)
    tornado.ioloop.IOLoop.configure(TornadoUvloop)
    tornado.ioloop.IOLoop.current().start()

依赖

aiohttp==3.3.2
async-timeout==3.0.0
attrs==18.1.0
chardet==3.0.4
idna==2.7
multidict==4.3.1
tornado==5.0.2
uvloop==0.10.2
yarl==1.2.6

看来python3.7的asyncio的速度有很大的提升

aiohttp asyncio callback linux tornado

aiohttp 类 tornado 代码风格模式去开发 !!!完美阿!!!!

使用aiohttp进行tornado代码风格的开发

仔细读了一下 aiohttp 的文档

http://aiohttp.readthedocs.io/en/stable/index.html

= = 竟然!!!

竟然有了!!!!!

类tornado开发特色的!!! 贼开心

from aiohttp import web
class basic(web.View):
    def out(self,text):
        return web.Response(text=text)
class Index(basic):
    async def get(self):
        name = self.request.match_info.get('name', "Anonymous")
        text = "Hello, " + name
        return self.out(text=text)
app = web.Application()
app.add_routes([web.view('/', Index),])
if __name__ == "__main__":
    web.run_app(app)