Does asyncio from python support coroutine-based API for UDP networking?

前端 未结 2 1153
一向
一向 2021-02-13 22:46

I was browsing the python asyncio module documentation this night looking for some ideas for one of my course projects, but I soon find that there might be a lack o

2条回答
  •  执念已碎
    2021-02-13 22:57

    The reason a stream-based API is not provided is because streams offer ordering on top of the callbacks, and UDP communication is inherently unordered, so the two are fundamentally incompatible.

    But none of that means you can't invoke coroutines from your callbacks - it's in fact quite easy! Starting from the EchoServerProtocol example, you can do this:

    def datagram_received(self, data, addr):
        loop = asyncio.get_event_loop()
        loop.create_task(self.handle_income_packet(data, addr))
    
    async def handle_income_packet(self, data, addr):
        # echo back the message, but 2 seconds later
        await asyncio.sleep(2)
        self.transport.sendto(data, addr)
    

    Here datagram_received starts your handle_income_packet coroutine which is free to await any number of coroutines. Since the coroutine runs in the "background", the event loop is not blocked at any point and datagram_received returns immediately, just as intended.

提交回复
热议问题