TLDR; I want to not immediately return from connection_made function within asyncio.Protocol as i want to implement some time taking checks that should ideally be awaited on within the connection_made function and run transport.abort() right within the function if the checks fail.
I don't know if asyncio.Protocol implementing its handler functions as synchronous functions is a flaw or not.
I've been trying to make changes to a codebase that we have that implements the asyncio.Protocol , and have been experiencing some difficulties in understanding that i won't be able to await on functions inside the non async def connection_made(transport)
function.
eg:
import asyncio
import time
def check(peername):
time.sleep(10) # await asyncio.sleep(10)
return False # or True
class EchoServerProtocol(asyncio.Protocol):
def connection_made(self, transport):
peername = transport.get_extra_info('peername')
print('Connection from {}'.format(peername))
if not check(peername):
transport.abort()
return
self.transport = transport
def data_received(self, data):
message = data.decode()
print('Data received: {!r}'.format(message))
print('Send: {!r}'.format(message))
self.transport.write(data)
print('Close the client socket')
self.transport.close()
What I would like to do is to make the check(peer) function an asynchronous method and then await on it so that other connections can be made simultaneously while the check function is still running in the event loop (as it is doing an io operation, not a cpu calculation).
I don't want to return from connection_made without confirming the genuineness of the connection as that could potentially allow the clients to send data when i still haven't verified if the connection is genuine.
I know that i can make check as an async function and call it within asyncio.ensure_future, but that would mean that i would be returning from the connection_made function without the proper checks.
My main reason for wanting to do this is because i have observed that the loop.create_server will not accept any other connections if the previous connection_made has been blocked.
Let me know if using ensure_future is the only way to go while returning from the connection_made function. Or if there is a way to handle IO blocking calls in connection_made without it preventing other connections from being made.