关于文章 asyncio

asyncio linux python socket

python3.7的asyncio的socket server

python3.7的asyncio的socket server

猝不及防的看了一下文档 发现出现这个简单实用的语法糖?

import asyncio

async def client_connected(reader:asyncio.StreamReader, writer: asyncio.StreamWriter):
    e=await reader.read(10*1024*1024)
    print(e)
    writer.write(b"200 hello world")
    await writer.drain()
    writer.close()

async def main(host, port):
    srv = await asyncio.start_server(
        client_connected, host, port)
    await srv.serve_forever()

if __name__ == "__main__":
    asyncio.run(main('127.0.0.1', 8080))
asyncio eventloop python 多线程 异步

python 中的run_in_executor 和 run_on_executor 实现在异步中调用阻塞函数

python 异步是怎样练成的!

有个挺好玩的东西 python中asyncio 有两个函数 run_in_executorrun_on_executor

run_on_executor 用法

##这部分懒得写demo了 随意找地方复制的 = =

from concurrent.futures import ThreadPoolExecutor
from tornado.ioloop import IOLoop
from tornado.concurrent import run_on_executor

class SleepHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(10)
    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self):
        start = time.time()
        res = yield self.sleep()
        self.write("when i sleep %f s" % (time.time() - start))
        self.finish()

    @run_on_executor
    def sleep(self):
        time.sleep(5)
        return 5

run_in_executor 用法

class Index(basic):
    async def get(self):
        name =  self.request.match_info.get('name', "Anonymous")
        text = "Hello, " + name
        loop = asyncio.get_event_loop()
        print(loop.__hash__())
        c = await  loop.run_in_executor(None,fuck,"asasas")
        return self.outJson(text=c)

def fuck(data)->str:
    return data+"async!!!"

似乎差别不是很大 = =

继续折腾!

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)
aiozmq async asyncio python rpc zeromq zmq

aiozmq 最简单使用 ,应该可以用在tornado 同步驱动转异步操作

基于zeromq的rpc组件 ,用起来比zeromq更简单

服务端

import asyncio
from aiozmq import rpc
import random

class Handler(rpc.AttrHandler):
    @rpc.method
    def remote(self, arg1, arg2):
        asyncio.sleep(random.randint(0,9))
        return arg1 + arg2

@asyncio.coroutine
def go():
    server =  yield from rpc.serve_rpc(Handler(),
                                       bind='ipc://a')
    yield from server.wait_closed()
if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(go())
    print("DONE")

客户端

import asyncio
from aiozmq import rpc

@asyncio.coroutine
def go():
    client = yield from rpc.connect_rpc(connect='ipc://a')
    for a in range(1,50000):
        ret = yield from client.call.remote(a, 2)
        print(ret)

asyncio.get_event_loop().run_until_complete(go())

最简单的demo- - 应该还能结合 zeromq的router 进行分布式多进程 worker

asyncio nodejs python uvloop 事件 异步

tornado使用uvloop执行

tornado使用uvloop执行异步

asyncio 是Python3.4 之后引入的标准库的,这个包使用事件循环驱动的协程实现并发。

uvloop 是 基于libuv 代替 asyncio 内事件循环的库

livbuv 则是大名鼎鼎的nodejs使用的io库

= =

自打知道这玩意就想用上

上代码

import tornado.ioloop
import tornado.web
from .router import Url
from core.service import log
#入口文件
settings = {
    'template_path': 'views',
    'static_path': 'static',
    'static_url_prefix': '/static/',
    "cookie_secret" : "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
    "xsrf_cookies": False,
}

import uvloop

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

    @staticmethod
    def main(port):
        application = tornado.web.Application(
            Url,
            **settings,)
        application.listen(port)
        tornado.ioloop.IOLoop.configure(TornadoUvloop)
        tornado.ioloop.IOLoop.current().start()