使用Python的asyncio设计异步API

时间:2016-06-25 08:43:12

标签: python asynchronous netty python-asyncio aio

我曾经在Java中使用Netty,我非常喜欢它的概念和它的工作方式。现在,我正在开发一个Python项目,我需要设计一个能够在许多不同的传输层上异步执行IO操作的API。

我决定创建类似于Netty的东西。当然我理解Netty是一个非常庞大的项目,我永远无法模仿它的精彩功能,但我只想在Python的asyncio之上实现一些基础。< / p>

这是我到目前为止所得到的:

from .utility import Next

__all__ = [
    "ChannelHandler", "InboundHandler", "OutboundHandler",
    "Pipeline", "Channel"
]

class ChannelHandler:

    async def connected(self, channel):
        raise NotImplementedError()

    async def raised(self, channel, exception):
        raise NotImplementedError()

    async def disconnected(self, channel):
        raise NotImplementedError()

class InboundHandler:

    async def received(self, channel, data, next):
        raise NotImplementedError()

class OutboundHandler:

    async def sent(self, channel, message, next):
        raise NotImplementedError()

class Pipeline:

    def __init__(self, channel, channel_handler, inbound_handlers, outbound_handlers):
        self.channel = channel
        self.channel_handler = channel_handler
        self.inbound_handlers = inbound_handlers
        self.outbound_handlers = outbound_handlers

    async def handle_raise_event(self, exception):
        await self.channel_handler.raised(channel, exception)

    async def handle_receive_event(self, data):
        await Next(self.inbound_handlers).next(self.channel, data)

    async def handle_send_event(self, message):
        await Next(self.outbound_handlers).next(self.channel, message)

class Channel:

    def __init__(self, url, channel_handler, inbound_handlers, outbound_handlers, options={}):
        self.url = url
        self.pipeline = Pipeline(self, channel_handler, inbound_handlers, outbound_handlers)
        self.options = options

    async def connect(self):
        pass

    async def send(self, message):
        pass

    async def disconnect(self):
        pass

async def connect(url, channel_handler, inbound_handlers, outbound_handlers, options={}):
    pass

这是核心API模块。以下是如何使用它的示例:

import asyncio
import transport

class DummyChannelHandler:

    async def connected(self, channel):
        print("Connected.")

    async def raised(self, channel, exception):
        print("Error:", exception)
        await channel.disconnect()

    async def disconnected(self, channel):
        print("Disconected.")

class DummyInboundHandler:

    async def received(self, channel, data, next):
        print("Received:", data)

class DummyOutboundHandler:

    async def sent(self, channel, message, next):
        await channel.write(message.encode())

async def main():
    channel = await transport.connect(
        "udp://192.168.20.244:55300",
        DummyChannelHandler(),
        [DummyInboundHandler()],
        [DummyOutboundHandler()]
    )

    await channel.send("Hi!")

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()

正如我所说,我对asyncio很新。我想问你关于你的意见,我真的很感激一些建议。是的,这是asyncio应该使用的方式吗?还是我完全忽略了这一点?这可能有用吗?那些同步问题怎么样?我知道像锁,队列这样的东西,但我不清楚如何使用asyncio的那些。

我不知道API是否设计正确。例如,我不确定关键字async是否用于正确的位置。与await相同。

如果你碰巧遇到了什么,请给我发表评论,谢谢!

0 个答案:

没有答案