您只需调用asyncio.open_connection(server, port)
协程一次,然后继续使用读取器和写入器(当然,前提是服务器不会在其端口关闭连接)。
我建议您为连接创建一个单独的异步上下文管理器对象,并使用连接池来管理连接,以便为许多并发任务创建和重复使用套接字连接。通过使用(异步)上下文管理器,Python会在您的代码完成时通知连接,以便将连接释放回池中:
import asyncio
import contextlib
from collections import OrderedDict
from types import TracebackType
from typing import Any, List, Optional, Tuple, Type
try:
base = contextlib.AbstractAsyncContextManager
except AttributeError:
base = object
Server = str
Port = int
Host = Tuple[Server, Port]
class ConnectionPool(base):
def __init__(
self,
max_connections: int = 1000,
loop: Optional[asyncio.AbstractEventLoop] = None,
):
self.max_connections = max_connections
self._loop = loop or asyncio.get_event_loop()
self._connections: OrderedDict[Host, List["Connection"]] = OrderedDict()
self._semaphore = asyncio.Semaphore(max_connections)
async def connect(self, server: Server, port: Port) -> "Connection":
host = (server, port)
await self._semaphore.acquire()
connections = self._connections.setdefault(host, [])
connection = next((conn for conn in connections if not conn.in_use), None)
if connection is None:
for conns_per_host in reversed(self._connections.values()):
for conn in conns_per_host:
if not conn.in_use:
await conn.close()
break
reader, writer = await asyncio.open_connection(server, port)
connection = Connection(self, host, reader, writer)
connections.append(connection)
connection.in_use = True
self._connections.move_to_end(host, False)
return connection
async def close(self):
"""Close all connections"""
connections = [c for cs in self._connections.values() for c in cs]
self._connections = OrderedDict()
for connection in connections:
await connection.close()
def _remove(self, connection):
conns_for_host = self._connections.get(connection._host)
if not conns_for_host:
return
conns_for_host[:] = [c for c in conns_for_host if c != connection]
def _notify_release(self):
self._semaphore.release()
async def __aenter__(self) -> "ConnectionPool":
return self
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
tb: Optional[TracebackType],
) -> None:
await self.close()
def __del__(self) -> None:
connections = [repr(c) for cs in self._connections.values() for c in cs]
if not connections:
return
context = {
"pool": self,
"connections": connections,
"message": "Unclosed connection pool",
}
self._loop.call_exception_handler(context)
class Connection(base):
def __init__(
self,
pool: ConnectionPool,
host: Host,
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
):
self._host = host
self._pool = pool
self._reader = reader
self._writer = writer
self._closed = False
self.in_use = False
def __repr__(self):
host = f"{self._host[0]}:{self._host[1]}"
return f"Connection<{host}>"
@property
def closed(self):
return self._closed
def release(self) -> None:
self.in_use = False
self._pool._notify_release()
async def close(self) -> None:
if self._closed:
return
self._closed = True
self._writer.close()
self._pool._remove(self)
try:
await self._writer.wait_closed()
except AttributeError:
pass
def __getattr__(self, name: str) -> Any:
"""All unknown attributes are delegated to the reader and writer"""
if self._closed or not self.in_use:
raise ValueError("Can't use a closed or unacquired connection")
if hasattr(self._reader, name):
return getattr(self._reader, name)
return getattr(self._writer, name)
async def __aenter__(self) -> "Connection":
if self._closed or not self.in_use:
raise ValueError("Can't use a closed or unacquired connection")
return self
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
tb: Optional[TracebackType],
) -> None:
self.release()
def __del__(self) -> None:
if self._closed:
return
context = {"connection": self, "message": "Unclosed connection"}
self._pool._loop.call_exception_handler(context)
然后将一个池对象传递给您的查找协程;连接对象产生了读取器和写入器部分的代理:
async def lookup(pool, server, port, query):
try:
conn = await pool.connect(server, port)
except (ValueError, OSError):
return {}
async with conn:
conn.write(query.encode("ISO-8859-1"))
await conn.drain()
data = b""
while True:
d = await conn.read(4096)
if not d:
break
data += d
data = data.decode("ISO-8859-1")
return data
请注意,
标准WHOIS协议(RFC 3912或其前身)规定每次查询后连接都会关闭。如果您连接到端口43上的标准WHOIS服务,则没有重用套接字的必要。
在这种情况下,读取器将已经到达了EOF(
reader.at_eof()
为true),任何进一步的读取尝试都将简单地返回空值(
reader.read(...)
将始终返回一个空的
b''
值)。在远程方超时终止套接字连接之前,向写入器写入不会出现错误。您可以随意向连接写入,但WHOIS服务器只会忽略查询。
reader.read()
返回一个空值时结束,那么您几乎肯定正在使用标准的whois连接。 - Martijn Pieters