doc中,使用事件循环,袜子必须是非阻塞的 . 但是如果它被阻止,我该怎么做才能在asyncio事件循环中读取这个套接字 .

我尝试使用loop.add_reader,但回调函数不能是协程 .

我想出了解决这种情况的解决方案,但我不确定它是最好的 .

import asyncio
from asyncio import events

loop = events.get_event_loop()

loop.add_reader(blocking_sock, read_data, sock)

def read_data(sock):
   """this callback function will only be a function"""
   data = sock.recv(1024)
   asyncio.ensure_future(handle_data_coroutine(data))

async def handle_data_coroutine(data)
    await do_something()